2

I have a pretty simple REST API project using Spring Boot 1.5.7 and Jackson 2.8.

Problem

I want Jackson to complain and raise an exception if there is an unknown field in the request. Although I have not set up anything telling it to ignore them, it simply ignores it and I do not know why.

Request:

{
  "appId":1,
  "version":"1.0",
  "status":true,
  "locked":false,
  "field": "it is ignored, but it must not be"
}

Requet method

@PutMapping
public ResponseEntity<?> updateAppToggle(@RequestBody AppToggleUpdateRequest request) {
   AppToggle updatedAppToggle = service.update(request);
   return new ResponseEntity<AppToggleResponse>
       (AppToggleResponse.fillResponse(updatedAppToggle), HttpStatus.OK);
   }

Request class

@Data
public class AppToggleUpdateRequest {

   @NotNull
   @Getter
   private Long appId;

   @NotNull
   @Getter
   private String version;

   private Boolean status;

   private Boolean locked;
}

My application.properties does not contain anything special, just datasource and a few stuff for H2 in-memory database.

I really do not know what is going on. Any help would be truly appreciated.

StatelessDev
  • 294
  • 4
  • 15
  • Basically, that's weird because the spec explicitly says that `FAIL_ON_UNKNOWN_PROPERTIES` is on by default. Are you sure none of those: https://howtoprogram.xyz/2017/02/18/ignore-exclude-new-fields-deserializing-jackson/ apply to your code? – Piotr Wilkin Nov 17 '17 at 16:56
  • As I said, my application is pretty simple (it´s for learning purposes) and I'm using everything out-of-the-box. I could try a solution like [this](https://stackoverflow.com/a/22874949/4319922), but it doesn't make any sense, once, as you said, the behavior I expect is set by default, although it doesn't appear to be in my application. – StatelessDev Nov 17 '17 at 17:03

2 Answers2

3

I fixed my application adding the following to the application.properties file:

spring.jackson.deserialization.FAIL_ON_UNKNOWN_PROPERTIES=true
StatelessDev
  • 294
  • 4
  • 15
  • 1
    This is necessary because Spring Boot, in the interests of being relaxed about what is accepted and strict about what is produced, disables failing on unknown properties: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-customize-the-jackson-objectmapper – Andy Wilkinson Nov 18 '17 at 07:30
0

Have you tried this:

@JsonIgnoreProperties(ignoreUnknown = true) 
@Data
public class AppToggleUpdateRequest { .. }
sagarr
  • 1,152
  • 1
  • 9
  • 16
  • I have tried to set it to "false" because I want my application to accept only known fields, but to no avail either. – StatelessDev Nov 17 '17 at 17:19