Eclipse + Spring + maven Building a RESTful Web Service ---需要添加注释
1.Eclipse --> help --> new software 安装Maven插件
url:http://download.eclipse.org/technology/m2e/releases
2.Eclipse 创建 Maven project (勾选 create a simple project)
groupId ,artifactId 为必填项,项目新建成功后对应 pom.xml 中的 <groupId >和<artifactId >中的值
3.修改pom.xml
添加配置文件:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-release</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-release</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
4.添加实体类:
public class Greeting {
private final long id;
private final String content;
public Greeting(long id,String content){
this.id = id;
this.content = content;
}
public long getId() {
return id;
}
public String getContent() {
return content;
}
}
5.添加Controller:
@RestController
public class GreetingController {
private static final String template = "hello,%s!";
private final AtomicLong counter = new AtomicLong();
@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name",defaultValue="world")String name){
return new Greeting(counter.getAndIncrement(),String.format(template, name));
}
}
6.添加项目启动类:
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
7.项目启动访问url :http://localhost:8080/greeting 返回结果:
{"id":1,"content":"hello,world!"}
测试成功:(Maven + Restful)学习阶段,暂且不加注释
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。