gpt4 book ai didi

spring-boot - JsonIgnoreProperties 不适用于 spring boot

转载 作者:行者123 更新时间:2023-12-02 20:25:00 26 4
gpt4 key购买 nike

目前Spring Boot示例应用创建正常。在请求中,如果有任何未知字段到来,那么我们需要抛出一个错误。

为此使用了 @JsonIgnoreProperties(ignoreUnknown = false) 注解。但是,当我访问该 URL 时,它不起作用。

请查找如下代码片段:

    @RestController @RequestMapping(value = "/")
@JsonIgnoreProperties(ignoreUnknown = false) public class
UserController {
private final Logger LOG = LoggerFactory.getLogger(getClass());

private final UserRepository userRepository;

private final UserDAL userDAL;

public UserController(UserRepository userRepository, UserDAL userDAL){
this.userRepository = userRepository;
this.userDAL = userDAL;
}

@RequestMapping(
value = "/create", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE
)
public User addNewUsers(@RequestBody @Valid User user)
throws JsonProcessingException {


LOG.info("Saving user.");

CardInfo cardInfo = new CardInfo();
cardInfo.setCardId("12345678901");
user.setCardInfo(cardInfo);
ObjectMapper mapper = new ObjectMapper();

String jsonString = mapper.writeValueAsString(cardInfo);
user.setCardInfo1(jsonString);
userDAL.getAllUsers();

return userRepository.save(user);
}

请查找示例Pom如下:http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0

<groupId>com.journaldev.spring</groupId>
<artifactId>spring-boot-mongodb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>spring-boot-mongodb</name>
<description>Spring Boot MongoDB Example</description>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

最佳答案

您至少有 3 个选择:

  1. @JsonIgnoreProperties 放在您反序列化的类上,而不是在 Spring Controller 中。

    • 但是,我看到您要反序列化的类是 com.journaldev.bootifulmongodb.model.User,因此您很可能无法修改它。
  2. 配置您的 ObjectMapper实例:

   mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
  1. Customize Spring Boot's Jackson Object Mapper - 通过设置正确的环境属性:

    spring.jackson.deserialization.fail-on-unknown-properties=true

更多信息请引用section 76.3 of Spring Boot's reference .

关于spring-boot - JsonIgnoreProperties 不适用于 spring boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50237435/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com