spring-guide之rest-service

来源:互联网 发布:先创网络 编辑:程序博客网 时间:2024/05/21 05:40

spring-guide之rest-service

目标:创建一个spring的符合restful规范的web service项目,并做出hello world。

构建一个简单的符合restful规范的web服务

将构建一个接受http get请求的web服务。

http://localhost:8080/greeting

并且返回一个json格式的响应

{"id":1,"content":"Hello, World!"}

同样,可以在url请求上自定义name参数,用来覆盖 World的值。、

{"id":1,"content":"Hello, User!"}

需要准备的

  1. 大概15分钟
  2. 一个友好的文本编辑器或者IDE
  3. jdk1.8 或者更晚的(我用的1.7)
  4. gradle2.3+ 或者maven3.0+ (我用的gradle)
  5. 也可以直接从github上导入代码

    官方:git clone https://github.com/spring-guides/gs-rest-service.git
    我的:https://github.com/chenlisong/projects/tree/master/gs-rest-service

gradle的一些配置

  1. gradle的快速学习

    官网:https://gradle.org/
    快速的使用gradle创建一个java项目:http://spring.io/guides/gs/gradle/

  2. 创建一个目录

    mkdir -p src/main/java/hello;└── src    └── main        └── java            └── hello
  3. 创建gradle的配置文件build.gradle

    buildscript {    repositories {        mavenCentral()    }    dependencies {        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.4.0.RELEASE")    }}apply plugin: 'java'apply plugin: 'eclipse'apply plugin: 'idea'apply plugin: 'spring-boot'jar {    baseName = 'gs-rest-service'    version =  '0.1.0'}repositories {    mavenCentral()}//我在这里将jdk改成1.7,原值1.8sourceCompatibility = 1.7targetCompatibility = 1.7dependencies {    compile("org.springframework.boot:spring-boot-starter-web")    testCompile('org.springframework.boot:spring-boot-starter-test')}
  4. 使用spring-boot启动web服务

    1. 使用方便简单
    2. 查找main方法来执行
    3. 解决依赖的jar包

代码实现

目标:通过项目和build系统,来创建一个简单的web服务。

服务将提供/greeting 类型是http get的接口,参数名是name,类型是string,用来替换world。

返回值如下

{    "id": 1,    "content": "Hello, World!"}

id是调用次数。

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;    }}
spring使用jackson json的jar包,自动把Greeting类解析成为json格式的字符串

创建一个Controller
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;@RestControllerpublic 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));    }}

再创建一个启动器,用spring-boot来启动。

src/main/java/hello/Application.java

package hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class Application {    public static void main(String[] args) {        SpringApplication.run(Application.class, args);    }}

构建成jar包的方式并执行

    //启动服务    ./gradlew bootRun -info | debug     //build成为jar包,并放在build/libs下    ./gradlew build    //cmd下执行jar并启动服务    java -jar build/libs/gs-rest-service-0.1.0.jar

测试一下

 request:http://localhost:8080/greeting response:{"id":1,"content":"Hello, World!"} request:http://localhost:8080/greeting?name=User response:{"id":2,"content":"Hello, User!"}
1 0