码迷,mamicode.com
首页 > 编程语言 > 详细

Eclipse + Spring + maven Building a RESTful Web Service ---需要添加注释

时间:2015-03-02 23:51:12      阅读:203      评论:0      收藏:0      [点我收藏+]

标签:

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)学习阶段,暂且不加注释

 

Eclipse + Spring + maven Building a RESTful Web Service ---需要添加注释

标签:

原文地址:http://www.cnblogs.com/summer-arvin/p/4309546.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!