Java Spring Boot 在控制器自动 JSON 到对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40247556/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 22:32:39  来源:igfitidea点击:

Spring Boot Automatic JSON to Object at Controller

javajsonspring-mvcspring-bootHymanson

提问by kamaci

I have SpringBoot application with that dependencies:

我有具有该依赖项的 SpringBoot 应用程序:

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

I have a method at my controller as follows:

我的控制器有一个方法如下:

@RequestMapping(value = "/liamo", method = RequestMethod.POST)
@ResponseBody
public XResponse liamo(XRequest xRequest) {
    ...
    return something;
}

I send a JSON object from my HTML via AJAX with some fields of XRequest type object (it is a plain POJO without any annotations). However my JSON is not constructed into object at my controller method and its fields are null.

我通过 AJAX 从我的 HTML 发送一个 JSON 对象,其中包含一些 XRequest 类型对象的字段(它是一个没有任何注释的普通 POJO)。但是,我的 JSON 未在我的控制器方法中构造为对象,并且其字段为空。

What I miss for an automatic deserialisation at my controller?

我想念我的控制器自动反序列化什么?

采纳答案by so-random-dude

Spring boot comes with Hymanson out-of-the-box which will take care of un-marshaling JSON request body to Java objects

Spring Boot 带有开箱即用的 Hymanson,它将负责将 JSON 请求正文解组到 Java 对象

You can use @RequestBody Spring MVC annotation to deserialize/un-marshall JSON string to Java object... For example.

您可以使用 @RequestBody Spring MVC 注释将 JSON 字符串反序列化/解组为 Java 对象...例如。

Example

例子

@RestController
public class CustomerController {
    //@Autowired CustomerService customerService;

    @RequestMapping(path="/customers", method= RequestMethod.POST)
    @ResponseStatus(HttpStatus.CREATED)
    public Customer postCustomer(@RequestBody Customer customer){
        //return customerService.createCustomer(customer);
    }
}

Annotate your entities member elements with @JsonProperty with corresponding json field names.

使用带有相应 json 字段名称的 @JsonProperty 注释您的实体成员元素。

public class Customer {
    @JsonProperty("customer_id")
    private long customerId;
    @JsonProperty("first_name")
    private String firstName;
    @JsonProperty("last_name")
    private String lastName;
    @JsonProperty("town")
    private String town;
}

回答by Paramvir Singh Karwal

SpringBoot by default comes with this functionality. You just have to use @RequestBodyannotation in parameter declaration of your controller method but in contrast to @so-random-dude's answeryou don't have to annotate fields with @JsonProperty, that is not required.

SpringBoot 默认自带这个功能。您只需要@RequestBody在控制器方法的参数声明中使用注释,但与@so-random-dude答案相反,您不必用 注释字段@JsonProperty,这不是必需的。

You just have to provide getters and setters for your custom XML object class. I am posting an example below for simplicity.

您只需要为您的自定义 XML 对象类提供 getter 和 setter。为简单起见,我在下面发布了一个示例。

Example:

例子:

Controller method declaration:-

控制器方法声明:-

@PostMapping("/create")
    public ResponseEntity<ApplicationResponse> createNewPost(@RequestBody CreatePostRequestDto createPostRequest){
        //do stuff
        return response;
    }

Your custom XML object class:-

您的自定义 XML 对象类:-

public class CreatePostRequestDto {
    String postPath;

    String postTitle;

    public String getPostPath() {
        return postPath;
    }

    public void setPostPath(String postPath) {
        this.postPath = postPath;
    }

    public String getPostTitle() {
        return postTitle;
    }

    public void setPostTitle(String postTitle) {
        this.postTitle = postTitle;
    }
}