Can I customize Jackson error messages?

I have a JacksonMappingException that I use to create errors for the user:

 public Map<String, List<Map<String, String>>> getErrors(JsonMappingException e) { Map<String, List<Map<String, String>>> errors = new HashMap<>(); List<Map<String, String>> badFields = new ArrayList<>(); for (Reference ref: e.getPath()) { Map<String, String> badField = new HashMap<>(); badField.put("field", ref.getFieldName()); badField.put("description", e.getOriginalMessage()); badFields.add(badField); } errors.put("errors", badFields); return errors; } 

Which is great, with the exception of some error classes, some technical data leaks to the user, such as the structure of classes, etc., which is completely indecent:

 { "errors" : [ { "field" : "id", "description" : "Unrecognized field \"id\" (class motif.web.resource.UserResource$PutUser), not marked as ignorable" } ] } 

How can I description be more like Unrecognized field \"id\" without any technical mumbo jumbo?

+7
java jackson
source share
1 answer

JsonMappingException has several subclasses. You can perform instance checks for the exception class, and then create your own description.

+1
source share

All Articles