繁体   English   中英

Spring 引导 @RequestBody 默认 POJO 映射行为?

[英]Spring boot @RequestBody default POJO mapping behavior?

我有一个 java class 带有大写字段名称,其中一些带有下滚动,如下所示:

public class DATADto {
  private String UPPERCASE;
  private String UNDER_SCROLL;

  public String getUPPERCASE() { return UPPERCASE; }
  public void setUPPERCASE(String s) { UPPERCASE = s; }
  ...//setters and getters
}

and I used this in a rest endpoint that accepts json in a spring rest controller:

@RestController
@RequestMapping({"/api/path"})
public class MyRestController {
   @PostMapping(path = {"/Data"}, consumes = MediaType.APPLICATION_JSON_VALUE,
            produces = MediaType.APPLICATION_JSON_VALUE)

    public ResponseEntity<?> useDATADto(@RequestBody DATADto aDATADto ) {
     //do something
    }
}

我需要默认发送哪些 JSON 字段,为什么?

这将取决于 Jackson 属性命名策略。 默认值为LOWER_CAMEL_CASE ,因此您的请求正文应如下所示:

{
    "uppercase": "test",
    "under_scroll": "test"
}

有关 Jackson 命名策略的所有可能配置,请参阅文档«Class PropertyNamingStrategy»

如果您使用的是 Spring,您可以使用此属性来配置命名策略:

spring.jackson.property-naming-strategy

另一种可能的方式是 bean 配置:

@Bean
public Jackson2ObjectMapperBuilder jacksonBuilder() {
     Jackson2ObjectMapperBuilder jacksonMapper = new Jackson2ObjectMapperBuilder();
     jacksonMapper.propertyNamingStrategy(PropertyNamingStrategy.LOWER_CASE);
     return jacksonMapper;
}

附加说明:

您当前的命名方法不遵循 Java 代码约定。 如果您需要使用某些特定命名格式处理 JSON,最好在 POJO 的字段上使用@JsonProperty注释。

请看下面的例子:

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

@Data
public class DATADto {
    @JsonProperty("UPPERCASE")
    private String uppercase;
    @JsonProperty("UNDER_SCROLL")
    private String underScroll;
}

您应该使用此请求正文向/api/path/data发送发布请求:

{
    "uppercase": "YOUR_VALUE",
    "under_scroll": "YOUR_VALUE"
}

故事是这样的。。

Spring Boot 默认使用 Jackson ObjectMapper 对 Java 对象进行序列化和反序列化。

在这种情况下,序列化是指将 java 对象转换为 json,反序列化是相反的过程。

关于@RequestBody注解, 文档中写了以下内容:

指示方法参数的注释应绑定到 web 请求的主体。 请求的主体通过 HttpMessageConverter 传递,以根据请求的内容类型解析方法参数。 或者,可以通过使用 @Valid 注释参数来应用自动验证。

简而言之, @RequestBody注释告诉 Spring 将传入的请求正文反序列化为作为参数传递给处理程序方法的 object。 Spring 使用MessageConverter实现此目的

Since Spring Boot uses Jackson by default for serializing and deserializing request and response objects in your REST APIs, and Jackson uses MappingJackson2HttpMessageConverter , so that will be message converter implementation that spring will use. 您可以在此处阅读更多相关信息。

The important thing is that Jackson uses Java Bean naming conventions to figure out the json properties in a Java class. Acutally 它使用默认的PropertyNamingStrategy 以下是文档中写的内容:

在没有注册的自定义策略的情况下,使用默认的 Java 属性命名策略,它保留字段名称,并从方法中删除 set/get/is 前缀(以及小写的大写字符的初始序列)。

因此,由于您没有设置任何命名策略,它将使用默认策略。

因此,如果您发送这样的有效负载:

{
    "uppercase": "YOUR_VALUE",
    "under_scroll": "YOUR_VALUE"
}

那行不通,你会得到异常,因为 jackson 不会在你的 class 中找到under_scroll属性,它会寻找under_SCROLL ,因此这个有效载荷:

{
    "uppercase": "YOUR_VALUE",
    "under_SCROLL": "YOUR_VALUE"
}

将工作。

要更改默认PropertyNamingStrategy ,请查看这篇文章。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM