Home > other >  Catch exception of Json unknown fields
Catch exception of Json unknown fields

Time:11-26

colleagues! I'm using quakus and need to catch exceptions that occurs when I send request with wrong fields (FAIL_ON_UNKNOWN_PROPERTIES = true) and send it to the response . How Can I do this? Maybe I need to write custom deserializer of this object?

CodePudding user response:

Yes, that's right, I would like to display a list of invalid fields

OK, so here's what you can do ...

  1. You receive the request body as a String
  2. Then you try to deserealise it into an object using an ObjectMapper class in your controller.
  3. Then you need to catch the thrown exception and parse out the list of fields either from its fields (if they are there) or from the message.

See an example of an error message here. Also check the Javadoc for the UnrecognizedPropertyException

Does this solve your problem ? Let me know in the comments.

  • Related