I want to handle json to Object conversion differently on different @RequestMapping
in my Controller.
I believe if we add Jackson dependency in our spring-boot project it handles json to Object conversion and #spring.jackson.deserialization.fail-on-unknown-properties=true
property will make sure that conversion will fail if there is some unknown property present in the json (please correct me if I am wrong).
Can we tell jackson locally when to fail on unknown properties and when to ignore those property.
Following is code snippet to use a flag.
@GetMapping(value = "sample")
public @ResponseBody UserDTO test(@RequestParam String str, @RequestParam boolean failFast) {
ObjectMapper map = new ObjectMapper();
if( failFast) {
map.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, true);
} else {
map.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
UserDTO userDTO = null;
try {
userDTO = map.readValue(str, UserDTO.class);
} catch (IOException e) {
e.printStackTrace();
}
return userDTO;
}
I don't need it to be handled at runtime like i am doing using @RequestParam.
Is there some property using which i can use to mark mappings where to check for unknown properties and where to ignore them.
Edit: What I am looking for is to change an existing application to handle Unknown property per mapping. For example:
@PostMapping(value = "fail/fast")
public @ResponseBody UserDTO test(@FAIL_ON_UNKNOWN @RequestBody UserDTO userDTO, @RequestParam boolean failFast) {
..///processing...
return userDTO;
}
@PostMapping(value = "fail/safe")
public @ResponseBody UserDTO test( @RequestBody UserDTO userDTO, @RequestParam boolean failFast) {
..///processing...
return userDTO;
}
If some king of validation can be added per mapping then i don't need to change all existing mapping's to customise unknown property and code change will be minimum.