JsonIgnoreProperties not working with spring boot
You have at least 3 options:
-
Put
@JsonIgnoreProperties
on a class you deserialize, and not in Spring controller.- However, I see that the class you want to deserialize is
com.journaldev.bootifulmongodb.model.User
so, most probably, you can't modify it.
- However, I see that the class you want to deserialize is
Configure your ObjectMapper instance:
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
-
Customize Spring Boot's Jackson Object Mapper - by setting a correct environment property:
spring.jackson.deserialization.fail-on-unknown-properties=true
For further information, please refer to section 76.3 of Spring Boot's reference.
Related videos on Youtube
Jagrut Dalwadi
Updated on June 04, 2022Comments
-
Jagrut Dalwadi almost 2 years
Currently, the Spring Boot sample application is created normally. In the request, if there are any unknown fields coming, then we need to throw an error.
For this the
@JsonIgnoreProperties(ignoreUnknown = false)
annotation is being used. However, when I am accessing the URL, it is not working.Please find code snippet as follows:
@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); }
Please find sample Pom as follows: 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>
-
Jagrut Dalwadi almost 6 yearsOption#3 works. However, option#1 was not giving expected behavior.
-
rkrishnan over 3 yearsYes, option 3 is what works for me. #1 does not work.