spring-guide之rest-service

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.8
    sourceCompatibility = 1.7
    targetCompatibility = 1.7
    
    dependencies {
        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;

@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));
    }
}

再創建一個啓動器,用spring-boot來啓動。

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);
    }
}

構建成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!"}
發佈了59 篇原創文章 · 獲贊 8 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章