简体   繁体   中英

Jackson ignore @Size when serializing

I have a situation in which I want to return a request to a user if it fails validation along with an appropriate error message. The problem that I've run across is that I'm using Jackson to deal with the JSON request, and the failure in validation also causes Jackson to fail to serialize the message.

For instance, if I have this object:

 public class SomeRequest
 {
     @Size(min=1, max=10)
     private String someField;
     //getter and setter here
 }

...when I go to serialize when the field is invalid (let's say it has 11 characters in it)...

 ObjectMapper mapper = new ObjectMapper();
 output = mapper.writeValueAsString(someRequestInstance);

...I get a JsonProcessingException (Error(s) validating object). I've confirmed that a valid object has no problem with serialization. So my question is this : How do I get Jackson to ignore the javax.validation annotations when serializing?

Thanks in advance!

By default Jackson does not invoke bean validation (javax.validation.constraints) on JSON serialization. Either default behavior is overridden/customized or there's any kind of interceptor/hook which does that.

Research the stack trace in order to find out where exactly exception occurs and dig around that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM