1. 程式人生 > 其它 >Spring Boot文件閱讀筆記-構建Restful風格的WebService

Spring Boot文件閱讀筆記-構建Restful風格的WebService

技術標籤:JavaSpring Boot文件閱讀筆記javarestfulspring boot

Maven程式碼如下:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.10.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>cn.it1995</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>


    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

服務端通過處理/greeting的get方法,接收name這個string引數,返回200的json狀態。

body返回如下:

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

id這個域是greetting的主鍵,這個值是唯一的。content裡面是問候語。

Greeting.java程式碼如下:

package cn.it1995.demo;

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

程式通過適用JaksonJson庫,將Greeting自動整理成json,Jackson預設被包含在web starter的jar包中,也就是這個jar包裡面:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

使用@RestController建立Rest風格的WebService,GreetingController.java監聽了/greeting的GET方法,程式碼如下:

package cn.it1995.demo;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicLong;

@RestController
public class GreetingController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @GetMapping("/greeting")
    public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name){

        return new Greeting(counter.incrementAndGet(), String.format(template, name));
    }
}

@GetMapping:代表適用HTTP的get方法,引數為/greeting代表http裡面的url為/greeting,等同於@RequestMapping(method=GET)。

@RequestParam:為需要傳入的引數,defaultValue為設定預設值。

@RestController:等同於@Controller加@ResponseBody

這裡Greeting物件能夠轉換為json多虧了Jackson2。Spring的MappingJackson2HttpMessageConverter會將Greeting轉換為JSON。

程式執行截圖如下: