Spring boot JsonIgnoreProperties不适用于spring boot
目前,Spring引导示例应用程序是正常创建的。在请求中,如果有任何未知字段出现,那么我们需要抛出一个错误 为此,正在使用Spring boot JsonIgnoreProperties不适用于spring boot,spring-boot,jackson,Spring Boot,Jackson,目前,Spring引导示例应用程序是正常创建的。在请求中,如果有任何未知字段出现,那么我们需要抛出一个错误 为此,正在使用@JsonIgnoreProperties(ignoreUnknown=false)注释。但是,当我访问URL时,它不起作用 请查找以下代码段: @RestController@RequestMapping(value=“/”) @JsonIgnoreProperties(ignoreUnknown=false)公共类 用户控制器{ 私有最终记录器LOG=LoggerFact
@JsonIgnoreProperties(ignoreUnknown=false)
注释。但是,当我访问URL时,它不起作用
请查找以下代码段:
@RestController@RequestMapping(value=“/”)
@JsonIgnoreProperties(ignoreUnknown=false)公共类
用户控制器{
私有最终记录器LOG=LoggerFactory.getLogger(getClass());
私有最终用户存储库用户存储库;
私有最终用户dal UserDAL;
公共用户控制器(UserRepository UserRepository,UserDAL UserDAL){
this.userRepository=userRepository;
this.userDAL=userDAL;
}
@请求映射(
value=“/create”,method=RequestMethod.POST,
consumes=MediaType.APPLICATION\u JSON\u值,
products=MediaType.APPLICATION\u JSON\u值
)
公共用户addNewUsers(@RequestBody@Valid User)
抛出JsonProcessingException{
LOG.info(“保存用户”);
CardInfo CardInfo=新的CardInfo();
cardInfo.setCardId(“12345678901”);
user.setCardInfo(cardInfo);
ObjectMapper mapper=新的ObjectMapper();
字符串jsonString=mapper.writeValueAsString(cardInfo);
setCardInfo1(jsonString);
userDAL.getAllUsers();
返回userRepository.save(用户);
}
请查找以下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>
com.journaldev.spring
弹簧靴mongodb
0.0.1-快照
罐子
弹簧靴mongodb
SpringBootMongoDB示例
org.springframework.boot
spring启动程序父级
1.5.9.1发布
UTF-8
UTF-8
1.8
org.springframework.boot
spring启动程序数据mongodb
org.springframework.boot
SpringBootStarterWeb
org.springframework.boot
弹簧起动试验
测试
org.springframework.boot
springbootmaven插件
您至少有3个选项:
@JsonIgnoreProperties
放在反序列化的类上,而不是放在Spring控制器中
- 但是,我看到您想要反序列化的类是
,所以很可能您无法修改它com.journaldev.bootifulmongodb.model.User
mapper.configure(在未知属性上反序列化feature.FAIL,true);
有关更多信息,请参阅。选项3有效。但是,选项1没有给出预期的行为。是的,选项3对我有效。#1不起作用。