直接在Controller中处理JSON是可以的,因为Spring MVC的和@PostMapping都支持指定输入和输出的格式。如果我们想接收JSON,输出JSON,那么可以这样写:

    对应的Maven工程需要加入Jackson这个依赖:com.fasterxml.jackson.core:jackson-databind:2.11.0

    注意到@PostMapping使用consumes声明能接收的类型,使用produces声明输出的类型,并且额外加了@ResponseBody表示返回的String无需额外处理,直接作为输出内容写入HttpServletResponse。输入的JSON则根据注解@RequestBody直接被Spring反序列化为User这个JavaBean。

    使用curl命令测试一下:

    1. $ curl -v -H "Content-Type: application/json" -d '{"email":"bob@example.com"}' http://localhost:8080/rest
    2. > POST /rest HTTP/1.1
    3. > Host: localhost:8080
    4. > User-Agent: curl/7.64.1
    5. > Accept: */*
    6. > Content-Type: application/json
    7. > Content-Length: 27
    8. >
    9. < HTTP/1.1 200
    10. < Content-Type: application/json;charset=utf-8
    11. < Date: Sun, 10 May 2020 09:56:01 GMT
    12. {"restSupport":true}

    直接用Spring的Controller配合一大堆注解写REST太麻烦了,因此,Spring还额外提供了一个@RestController注解,使用@RestController替代@Controller后,每个方法自动变成API接口方法。我们还是以实际代码举例,编写ApiController如下:

    编写REST接口只需要定义@RestController,然后,每个方法都是一个API接口,输入和输出只要能被Jackson序列化或反序列化为JSON就没有问题。我们用浏览器测试GET请求,可直接显示JSON响应:

    要测试POST请求,可以用curl命令:

    1. $ curl -v -H "Content-Type: application/json" -d '{"email":"bob@example.com","password":"bob123"}' http://localhost:8080/api/signin
    2. > POST /api/signin HTTP/1.1
    3. > Host: localhost:8080
    4. > User-Agent: curl/7.64.1
    5. > Accept: */*
    6. > Content-Type: application/json
    7. > Content-Length: 47
    8. >
    9. < HTTP/1.1 200
    10. < Content-Type: application/json
    11. < Date: Sun, 10 May 2020 08:14:13 GMT
    12. <
    13. {"user":{"id":1,"email":"bob@example.com","password":"bob123","name":"Bob",...

    但是这样一来,如果写一个register(User user)方法,那么该方法的User对象也拿不到注册时用户传入的密码了。如果要允许输入password,但不允许输出password,即在JSON序列化和反序列化时,允许写属性,禁用读属性,可以更精细地控制如下:

    1. public class User {
    2. ...
    3. @JsonProperty(access = Access.WRITE_ONLY)
    4. public String getPassword() {
    5. return password;
    6. }
    7. ...
    8. }

    同样的,可以使用@JsonProperty(access = Access.READ_ONLY)允许输出,不允许输入。

    从下载练习:使用REST实现API (推荐使用快速下载)

    使用@RestController可以方便地编写REST服务,Spring默认使用JSON作为输入和输出。

    使用REST - 图1