http://blog.csdn.net/u010902721/article/details/51203444
不使用IDE工具,通过命令行快速构建一个基于Spring的RESTful Web Service。
需要的工具
1、maven(不会使用的话可以参考http://blog.csdn.net/u010902721/article/details/51203170)
2、文本编辑器。
开始
该工程会接受HTTP GET请求,请求网址是http://localhost:8080/greeting。
然后返回json格式数据{"id":1,"content":"Hello, World!"}
。
当url中含有参数时,例如http://localhost:8080/greeting?name=User
,返回的数据是{"id":1,"content":"Hello, User!"}
。
在项目根目录下创建子目录结构如下:
└── src
└── main
└── java
└── hello
在项目根目录下新建pom.xml,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.springframework</groupId>
<artifactId>gs-rest-service</artifactId>
<version>0.1.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.3.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</pluginRepository>
</pluginRepositories>
</project>
新建src/main/java/hello/Greeting.java,代码如下:
package hello;
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;
}
}
新建src/main/java/hello/GreetingController.java,代码如下:
package hello;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@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.incrementAndGet(),
String.format(template, name));
}
}
新建src/main/java/hello/Application.java, 代码如下:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
命令行转移到项目根目录下,windows下执行以下命令(linux下改一下/):
mvn package
mvn java -jar target/gs-rest-service-0.1.0.jar
打开浏览器输入http://localhost:8080/greeting查看结果。
参考文献:http://spring.io/guides/gs/rest-service/#scratch