Spring Boot Web:处理 HTTP 请求体中的 JSON 数据


#Spring Boot


一般 HTTP 客户端会通过 POST 方法向 HTTP 发送 JSON 数据。本文学习下如何解析。

示例

项目结构

.
├── build.gradle
└── src
    └── main
        ├── java
        │   └── hello
        │       ├── Application.java
        │       ├── BaseResponse.java
        │       ├── GreetingController.java
        │       ├── GreetingRequest.java
        │       └── GreetingResponse.java
        └── resources

build.gradle

buildscript {
    repositories {
        maven { url 'http://mirrors.cloud.tencent.com/nexus/repository/maven-public/' }
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.3.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web")
    compile group: 'org.projectlombok', name: 'lombok', version: '1.18.0'
}

BaseResponse 类

package hello;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.io.Serializable;

@Data
@AllArgsConstructor
public class BaseResponse implements Serializable {

    Boolean success;
    String msg;
    Object result;

    public static BaseResponse success(String msg, Object result) {
        return new BaseResponse(true, msg, result);
    }

    public static BaseResponse fail(String msg, Object result) {
        return new BaseResponse(false, msg, result);
    }

}

GreetingRequest 类

package hello;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class GreetingRequest {

    private String msg;

}

GreetingResponse 类

package hello;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class GreetingResponse {

    private long id;
    private String content;

}

Application 类:程序主入口

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

}

GreetingController 类

package hello;

import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.*;

@RestController
public class GreetingController {

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

    @RequestMapping(value = "/greeting1")
    public BaseResponse greeting1(@RequestBody GreetingRequest request) {
        return BaseResponse.success("", new GreetingResponse(counter.incrementAndGet(), String.format(template, request.getMsg())));
    }

}

@RequestBody注解用于将请求中的JSON转换为对象。

运行示例

运行 Application 类。使用 Postman 构建 POST 请求到http://127.0.0.1:8080/greeting1,请求体是json:

{"msg": "World"}

响应体:

{
    "success": true,
    "msg": "",
    "result": {
        "id": 1,
        "content": "Hello, World!"
    }
}


( 本文完 )