Spring Boot文档阅读笔记-构建Restful风格的WebService

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。

 

程序运行截图如下:

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页