您的位置:首页 > 编程语言 > Java开发

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

2015-03-02 22:32 513 查看
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

文档查考:http://maven.apache.org/pom.html#The_Basics

添加配置文件:

<parent>
<groupId>org.springframework.boot</groupId><!--This is generally unique amongst an organization or a project-->
<artifactId>spring-boot-starter-parent</artifactId><!--The artifactId is generally the name that the project is known by-->
<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)学习阶段,暂且不加注释
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: