Jackson json deserialization, with root element from json - java

I am having a question with Jackson that I think should be simple to solve, but it is killing me.
I have a java POJO class that looks like this (with getters and setters)
#JsonRootName(value = "notificacion")
public class NotificacionDTO extends AbstractDTO {
#JsonProperty(required=true)
private Integer instalacion;
#JsonProperty(required=true)
private Integer tipoNotificacion;
#JsonProperty(required=true)
private String mensaje;
}
Here is the AbstractDTO
public abstract class AbstractDTO implements Serializable {
public void validate() {
Field[] declaredFields = this.getClass().getDeclaredFields();
for (Field field : declaredFields) {
if (field.isAnnotationPresent(JsonProperty.class)){
if (field.getAnnotation(JsonProperty.class).required() && this.isNullParameter(field)) {
throw new RuntimeException(String.format("El parametro %s es null o no esta presente en el JSON.", field.getName()));
}
}
}
}
private boolean isNullParameter(Field field) {
try {
field.setAccessible(true);
Object value = field.get(this);
if (value == null) {
return true;
} else if (field.getType().isAssignableFrom(String.class)) {
return ((String) value).isEmpty();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return false;
}
}
I want to deserialize JSON that looks like this into a NotificacionDTO object:
{
"notificacion":
{
"instalacion":"1",
"tipoNotificacion":"2",
"mensaje":"Un Mensaje"
}
}
This is my EndPoint
#Controller
#RequestMapping("/notificacion")
public class NotificacionEndPoint extends AbstractEndPoint{
#Autowired
private NotificacionService service;
#RequestMapping(value = {"", "/"}, method = RequestMethod.POST)
#ResponseStatus(HttpStatus.CREATED)
public void addNotification(#RequestBody NotificacionDTO notification) throws JsonParseException, JsonMappingException, IOException {
this.log.info("[POST RECEIVED] = " + notification);
notification.validate();
this.service.addNotification(notification);
}
}
I hava a custom ObjectMapper with this
public class JsonObjectMapper extends ObjectMapper {
public JsonObjectMapper() {
super();
this.configure(DeserializationFeature.UNWRAP_ROOT_VALUE, true);
}
}
When i POST i'm getting this error
Caused by: com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "notificacion" (class ar.com.tecnoaccion.services.notificacion.NotificacionDTO), not marked as ignorable (3 known properties: , "tipoNotificacion", "instalacion", "mensaje"])
at [Source: org.apache.catalina.connector.CoyoteInputStream#1c1f3f7; line: 3, column: 5] (through reference chain: ar.com.tecnoaccion.services.notificacion.NotificacionDTO["notificacion"])
I try to add this to my DTO
#JsonIgnoreProperties(ignoreUnknown = true)
but when i validate my DTO with the method validate all the dto's attributes are null and i get this error:
java.lang.RuntimeException: El parametro instalacion es null o no esta presente en el JSON.
i am using jackson 2.2.3 and spring 3.2.1
thank you.

the simple answer is to post
{
"instalacion":"1",
"tipoNotificacion":"2",
"mensaje":"Un Mensaje"
}
instead of
{
"notificacion":
{
"instalacion":"1",
"tipoNotificacion":"2",
"mensaje":"Un Mensaje"
}
}

Related

Read JSON Nested element using reflection without using JsonFactory or ObjectMapper

[Unable to access property of another object stored in Arraylist]
I am creating an function to get JSON input in object from RESTful Web service input and format it again in JSON format to call other web service.
I have limitation that I can not use any JSON API for object mapping hence using Java reflection core API.
I am able to create JSON format from Input for simple elements but unable to access nested elements (another user defined POJO class ). I am using arraylist.
Input
{
"GenesisIncidents": {
"service": "Transmission",
"affectedCI": "22BT_ORNC03",
"opt_additionalAffectedItems": [
{
"itemType": "NODE-ID",
"ItemName": "22BT_ORNC03"
},
{
"ItemType": "CCT",
"ItemName": "A_circuit_id"
}]
}
}
GenesisIncidents.class
import java.util.ArrayList;
import java.util.Date;
public class GenesisIncidents {
private String service;
private String affectedCI;
private ArrayList<AdditionalAffectedItems> opt_additionalAffectedItems;
public GenesisIncidents(){}
public String getService() {
return service;
}
public void setService(String service) {
this.service = service;
}
public String getAffectedCI() {
return affectedCI;
}
public void setAffectedCI(String affectedCI) {
this.affectedCI = affectedCI;
}
public ArrayList<AdditionalAffectedItems> getOpt_additionalAffectedItems() {
return opt_additionalAffectedItems;
}
public void setOpt_additionalAffectedItems(ArrayList<AdditionalAffectedItems> opt_additionalAffectedItems) {
this.opt_additionalAffectedItems = opt_additionalAffectedItems;
}
}
AdditionalAffectedItems.class
public class AdditionalAffectedItems {
private String itemType;
private String itemName;
public AdditionalAffectedItems(){
super();
}
public String getItemType() {
return itemType;
}
public void setItemType(String itemType) {
this.itemType = itemType;
}
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
}
Implemetation
public void updateTicketExt(GenesisIncidents genesisIncidents) {
try{
Field allFields[]=genesisIncidents.getClass().getDeclaredFields();
Method allMethods[] = genesisIncidents.getClass().getDeclaredMethods();
String jsonString ="{\r\n \""+genesisIncidents.getClass().getName().toString().substring(48)+"\": {";
final String preStr="\r\n \""; //To create a JSON object format.
final String postStr="\": "; //To create a JSON object format.
int totalNoOfFields=allFields.length;
for (Field field : allFields) {
System.out.println(field.getType());
String getter="get"+StringUtils.capitalize(field.getName());
Method method= genesisIncidents.getClass().getMethod(getter, null);
try{
if(field.getType().toString().contains("Integer"))
jsonString=jsonString + preStr + field.getName() + postStr +method.invoke(genesisIncidents).toString()+",";
else
jsonString=jsonString + preStr + field.getName() + postStr +"\""+method.invoke(genesisIncidents).toString()+"\",";
if(field.getType().toString().contains("ArrayList")){
System.out.println("ArrayListElement found");
genesisIncidents.getOpt_additionalAffectedItems().forEach(obj->{System.out.println(obj.getItemName());});
//convertArrayToJSON(field, genesisIncidents);
}
}catch(NullPointerException npe)
{
System.out.println("Null value in field.");
continue;
}
}
jsonString=jsonString.substring(0,jsonString.length()-1);
jsonString=jsonString+"\r\n }\r\n }";
System.out.println("\n"+jsonString);
}catch(Exception jex){
jex.printStackTrace();
}
}
My below code line is unable to access object stored under array list.
genesisIncidents.getOpt_additionalAffectedItems().forEach(obj->{System.out.println(obj.getItemName());});
OUTPUT
karaf#root>class java.lang.String
class java.lang.String
class java.lang.String
class java.util.ArrayList
ArrayListElement found
null
null
{
"GenesisIncidents": {
"service": "Transmission",
"affectedCI": "22BT_ORNC03",
"opt_additionalAffectedItems": " [org.apache.servicemix.examples.camel.rest.model.AdditionalAffectedItems#5881a 895, org.apache.servicemix.examples.camel.rest.model.AdditionalAffectedItems#399b4e eb]"
}
}
I have fiddled around with your example I have managed to get it working. This will produce the correct JSON string by passing in an instance of a GenesisIncident object. I guess that there is much room for improvement here but this can serve as an example.
public static String genesisToJson(GenesisIncidents incidents) {
try{
StringBuilder jsonBuilder = new StringBuilder();
jsonBuilder.append("{\r\n \"")
.append(incidents.getClass().getSimpleName())
.append("\": {");
Field allFields[] = incidents.getClass().getDeclaredFields();
for (Field field : allFields) {
String getter = getGetterMethod(field);
Method method = incidents.getClass().getMethod(getter, null);
try{
if(field.getType().isAssignableFrom(Integer.class)) {
jsonBuilder.append(preStr).append(field.getName()).append(postStr)
.append(method.invoke(incidents).toString()).append(",");
} else if (field.getType().isAssignableFrom(String.class)) {
jsonBuilder.append(preStr).append(field.getName()).append(postStr).append("\"")
.append(method.invoke(incidents).toString()).append("\",");
} else if (field.getType().isAssignableFrom(List.class)) {
System.out.println("ArrayListElement found");
getInnerObjectToJson(field, incidents.getOptItems(), jsonBuilder);
}
} catch(NullPointerException npe) {
System.out.println("Null value in field.");
continue;
}
}
jsonBuilder.append("\r\n } \r\n }");
return jsonBuilder.toString();
}catch(Exception jex){
jex.printStackTrace();
}
return null;
}
private static void getInnerObjectToJson(Field field, List<AdditionalAffectedItems> items, StringBuilder builder)
throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
builder.append(preStr).append(field.getName()).append(postStr).append("[");
for (var item : items) {
var fields = List.of(item.getClass().getDeclaredFields());
builder.append("{");
for (var f : fields) {
String getter = getGetterMethod(f);
Method method = item.getClass().getMethod(getter, null);
builder.append(preStr).append(f.getName()).append(postStr).append("\"")
.append(method.invoke(item).toString()).append("\"");
if (!(fields.indexOf(f) == (fields.size() - 1))) {
builder.append(",");
}
}
if (items.indexOf(item) == (items.size() - 1)) {
builder.append("}\r\n");
} else {
builder.append("},\r\n");
}
}
builder.append("]");
}
private static String getGetterMethod(Field field) {
return "get" + StringUtils.capitalize(field.getName());
}

Jackson ContextualDeserializer does not deserialize all fields

I'm implementing a custom jackson deserializer for one of my entities.
My entity is the following:
#Value
#JsonDeserialize
#AllArgsConstructor
public class TestModel {
private final FieldUpdate<UUID> field1Update;
private final FieldUpdate<UUID> field2Update;
private final FieldUpdate<String> field3Update;
public String toString() {
return "TestModel. Field1="+(field1Update != null ? field1Update.toString() : null)+
" Field2="+(field2Update != null ? field2Update.getClass().getName() : null) +
" Field3="+(field3Update != null ? field3Update.getClass().getName() : null);
}
}
My problem is that serialiation works as expected - the successfully serialized object is as follow:
{
"field1Update" : {
"type" : "update",
"value" : "f59c4ef9-52c4-4f3d-99e5-a33a13ae12f3"
},
"field2Update" : {
"type" : "keep"
},
"field3Update" : {
"type" : "reset"
}
}
=> which is correct.
(There are the 3 Types Update, Keep and Reset). Only update needs a value.
The problem is: When i deserialize this, only the first field (field1Update) gets deserialized. The other 2 fields (field2Update and field3Update) are null after deserialization completes.
My Deserializer is the following:
public class FieldUpdateDeserializer extends StdDeserializer implements ContextualDeserializer {
private JavaType contentType;
public FieldUpdateDeserializer(JavaType contentType) {
this(null,contentType);
}
public FieldUpdateDeserializer() {
this(null,null);
}
public FieldUpdateDeserializer(Class<?> vc, JavaType contentType) {
super(vc);
this.contentType = contentType;
}
public JsonDeserializer<?> createContextual(DeserializationContext ctxt,
BeanProperty property) throws JsonMappingException {
JavaType t = property.getType();
JavaType boundType = t.getBindings().getBoundType(0);
return new FieldUpdateDeserializer(boundType);
}
#Override
public Object deserialize(JsonParser jp, DeserializationContext ctx) throws IOException {
if(!"type".equals(jp.nextFieldName()) )throw new JsonParseException(jp,"'type' expected");
String typeVal = jp.nextTextValue();
if("update".equals(typeVal)) {
jp.nextValue(); //consume type.
try {
JsonDeserializer deser = ctx.findNonContextualValueDeserializer(contentType);
return new Update<>(deser.deserialize(jp,ctx));
} catch (Exception ex) {
throw new IllegalStateException("Could not handle deserialization for type", ex);
}
} else if("keep".equals(typeVal)) {
return new Keep<>();
} else if("reset".equals(typeVal)) {
return new Reset<>();
} else {
return ctx.handleUnexpectedToken(FieldUpdate.class, jp);
}
}
}
An interesting fact is that jackson calls the deserialize(...) method only one time and i can't figure out why....
Glad if somebody can drop me a hint.
greetings,
Michael
Ok - after some sleep and analyzing what happens in the jackson serializer, i discovered that i did not consume enough tokens in my deserializer.
The working version for my deserializer is:
public Object deserialize(JsonParser jp, DeserializationContext ctx) throws IOException {
if(!"type".equals(jp.nextFieldName()) )throw new JsonParseException(jp,"'type' expected");
String typeVal = jp.nextTextValue();
if("update".equals(typeVal)) {
jp.nextValue(); //consume type.
try {
JsonDeserializer deser = ctx.findNonContextualValueDeserializer(contentType);
return new Update<>(deser.deserialize(jp,ctx));
} catch (Exception ex) {
throw new IllegalStateException("Could not handle deserialization for type", ex);
} finally {
jp.nextToken();
}
} else if("keep".equals(typeVal)) {
jp.nextToken();
return new Keep<>();
} else if("reset".equals(typeVal)) {
jp.nextToken();
return new Reset<>();
} else {
return ctx.handleUnexpectedToken(FieldUpdate.class, jp);
}
}

Java to Json validation using GSON

While converting Java object to Json string using GSON API, I also want to fail this Json conversion if any of the annotated attribute is null.
For example
public class Order{
#SerializedName("orderId")
#Expose
#Required
private Integer id;
//getter & setter available for id
}
Now as I am doing
Order order = new Order();
JSONObject jsonobj = new JSONObject(gson.toJson(order));
I want to fail the above Java to Json transformation if any of the #Required attribute is null
Is this possible using GSON?
I wanted to fail Java to Json conversion, if any of the Java attribute is null which is annotated as #Required,
I am able to achieve this using following approach. Please let me know if you see any issues:
class RequiredKeyAdapterFactory implements TypeAdapterFactory {
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
final TypeAdapter<T> delegate = gson.getDelegateAdapter(this, type);
return new TypeAdapter<T>() {
#Override
public void write(JsonWriter out, T value) throws IOException {
if (value != null) {
Field[] fields = value.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
if (fields[i]
.isAnnotationPresent(Required.class)) {
validateNullValue(value, fields[i]);
}
}
}
delegate.write(out, value);
}
private <T> void validateNullValue(T value, Field field) {
field.setAccessible(true);
Class t = field.getType();
Object v = null;
try {
v = field.get(value);
} catch (IllegalArgumentException | IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
if (t == boolean.class && Boolean.FALSE.equals(v)) {
throw new IllegalArgumentException(field + " is null");
} else if (t.isPrimitive()
&& ((Number) v).doubleValue() == 0) {
throw new IllegalArgumentException(field + " is null");
} else if (!t.isPrimitive() && v == null) {
throw new IllegalArgumentException(field + " is null");
}
}
#Override
public T read(JsonReader in) throws IOException {
return delegate.read(in);
}
};
}
}
RequiredKeyAdapterFactory requiredKeyAdapterFactory = new RequiredKeyAdapterFactory();
Gson gson = new GsonBuilder().registerTypeAdapterFactory(requiredKeyAdapterFactory)
.create();
This is working

Convert the java class data into JSON format?

I am doing the Java project with spring.So I am using the Jackson library to convert to get the JSON format.
My java Class will be ,
public class ChatInteraction extends Interaction{
private int ticketId;
private String name;
private String interactionType ;
private LinkedList<InteractionInfo> interactions;
public ChatInteraction(Message response) {
super(response);
interactions = new LinkedList<InteractionInfo>();
}
public int getTicketId() {
return ticketId;
}
public void setTicketId(int ticketId) {
this.ticketId = ticketId;
System.out.println("Ticket Id for Interaction : "+this.ticketId);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
System.out.println("Name for Interaction : "+this.name);
}
public LinkedList<InteractionInfo> getInteractions() {
return interactions;
}
public String getInteractionType() {
return interactionType;
}
public void setInteractionType(String interactionType) {
this.interactionType = interactionType;
}
public void addInteraction(InteractionInfo interaction) {
this.interactions.add(interaction);
}
public void accept(int proxyId,String intxnId,int ticketId){
RequestAccept reqAccept = RequestAccept.create();
reqAccept.setProxyClientId(proxyId);
reqAccept.setInteractionId(intxnId);
reqAccept.setTicketId(ticketId);
System.out.println("New Chat RequestAccept Request Object ::: "+reqAccept.toString());
try{
if(intxnProtocol.getState() == ChannelState.Opened){
Message response = intxnProtocol.request(reqAccept);
System.out.println("New Chat RequestAccept Response ::: "+response.toString());
if(response != null ){
if( response.messageId() == EventAck.ID){
System.out.println("Accept new chat success !");
//EventAccepted accept = (EventAccepted)response;
//return "New chat Interaction accepted";
}else if(response.messageId() == EventError.ID){
System.out.println("Accept new chat Failed !");
//return "New chat Interaction rejected";
}
}
}else{
System.out.println("RequestAccept failure due to Interaction protocol error !");
}
}catch(Exception acceptExcpetion){
acceptExcpetion.printStackTrace();
}
}
public void join(String sessionId, String subject) {
RequestJoin join = RequestJoin.create();
join.setMessageText(MessageText.create(""));
join.setQueueKey("Resources:"); //Add the chat-inbound-key in multimedia of the optional tab values of the softphone application in CME
join.setSessionId(sessionId);
join.setVisibility(Visibility.All);
join.setSubject(subject);
KeyValueCollection kvc = new KeyValueCollection();
join.setUserData(kvc);
System.out.println("Join Request Object ::: "+join.toString());
try {
if(basicProtocol != null && basicProtocol.getState() == ChannelState.Opened){
Message response = basicProtocol.request(join);
if(response != null){
System.out.println("RequestJoin response ::: "+response);
if (response.messageId() == EventSessionInfo.ID) {
System.out.println("Join Request success !");
}else{
System.out.println("Join Request Failed !");
}
}
}else{
System.out.println("BasicChat protocol Error !");
//return "BasicChat protocol Error !";
}
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
}
I need to get only the interactionType and interactions property of this class in the JSON format like ,
{"interactionType":"invite","interactions" : [{"xx":"XX","yy":"YY"},{"xx":"XX","yy":"YY"}]}
Note :
I don't need the other properties of this class.
Also there is no SETTER for the interactions property . Instead of that I have the addInteractions() method . Does this affects any behaviour of JSON conversion ?
Also I have some other methods like accept(...) , Join(...).
I am using the jackson-all-1.9.0.jar
You can annotate the unneeded fields with #JsonIgnore - see Jackson's manual on annotations. That's what it will look like, using your code:
public class ChatInteraction extends Interaction{
#JsonIgnore
private int ticketId;
#JsonIgnore
private String name;
private String interactionType ;
private LinkedList<InteractionInfo> interactions;
You can use achieve this by using the #JsonIgnoreProperties annotation that can be used on class level.
From JavaDoc:
Annotation that can be used to either suppress serialization of
properties (during serialization), or ignore processing of JSON
properties read (during deserialization).
Example:
// to prevent specified fields from being serialized or deserialized
// (i.e. not include in JSON output; or being set even if they were included)
\#JsonIgnoreProperties({ "internalId", "secretKey" })
Example, In your case:
#JsonIgnoreProperties({ "ticketId", "name" })
public class ChatInteraction extends Interaction{
....
}
Finally I got the solution by others answers in the thread and similar answers in stackoverflow,
I marked the #JsonIgnore in the unwanted field in the sub class and super class suggested by fvu.
I have used the myObjectMapper.setVisibility(JsonMethod.FIELD, Visibility.ANY); in my objectMapper suggested in other thread like,
ObjectMapper mapp = new ObjectMapper();
mapp.setVisibility(JsonMethod.FIELD, Visibility.ANY);
try {
json = mapp.writeValueAsString(info);
info.clear();
System.out.println("Chat Info in JSON String is :::> "+json);
} catch (Exception e) {
e.printStackTrace();
}

Jackson deserialization error handling

My problem is fairly simple : I have the following simple class:
public class Foo {
private int id = -1;
public void setId(int _id){ this.id = _id; }
public int getId(){ return this.id; }
}
And I am trying to process following JSON:
{
"id": "blah"
}
Obviously, there is a problem here ("blah" cannot be parsed to an int)
Formerly, Jackson throws something like org.codehaus.jackson.map.JsonMappingException: Can not construct instance of java.lang.Integer from String value 'blah': not a valid Integer value
I agree with this, but I'd like to register something somewhere allowing to ignore this type of mapping errors.
I tried with a DeserializationProblemHandler registered (see here) but it seems to only work on unknown properties and not deserialization problems.
Have you any clue on this issue?
I succeeded to solve my problem, thanks to Tatu from Jackson ML.
I had to use custom non blocking deserializers for every primitive types handled in Jackson.
Something like this factory :
public class JacksonNonBlockingObjectMapperFactory {
/**
* Deserializer that won't block if value parsing doesn't match with target type
* #param <T> Handled type
*/
private static class NonBlockingDeserializer<T> extends JsonDeserializer<T> {
private StdDeserializer<T> delegate;
public NonBlockingDeserializer(StdDeserializer<T> _delegate){
this.delegate = _delegate;
}
#Override
public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
try {
return delegate.deserialize(jp, ctxt);
}catch (JsonMappingException e){
// If a JSON Mapping occurs, simply returning null instead of blocking things
return null;
}
}
}
private List<StdDeserializer> jsonDeserializers = new ArrayList<StdDeserializer>();
public ObjectMapper createObjectMapper(){
ObjectMapper objectMapper = new ObjectMapper();
SimpleModule customJacksonModule = new SimpleModule("customJacksonModule", new Version(1, 0, 0, null));
for(StdDeserializer jsonDeserializer : jsonDeserializers){
// Wrapping given deserializers with NonBlockingDeserializer
customJacksonModule.addDeserializer(jsonDeserializer.getValueClass(), new NonBlockingDeserializer(jsonDeserializer));
}
objectMapper.registerModule(customJacksonModule);
return objectMapper;
}
public JacksonNonBlockingObjectMapperFactory setJsonDeserializers(List<StdDeserializer> _jsonDeserializers){
this.jsonDeserializers = _jsonDeserializers;
return this;
}
}
Then calling it like this way (pass as deserializers only those you want to be non blocking) :
JacksonNonBlockingObjectMapperFactory factory = new JacksonNonBlockingObjectMapperFactory();
factory.setJsonDeserializers(Arrays.asList(new StdDeserializer[]{
// StdDeserializer, here, comes from Jackson (org.codehaus.jackson.map.deser.StdDeserializer)
new StdDeserializer.ShortDeserializer(Short.class, null),
new StdDeserializer.IntegerDeserializer(Integer.class, null),
new StdDeserializer.CharacterDeserializer(Character.class, null),
new StdDeserializer.LongDeserializer(Long.class, null),
new StdDeserializer.FloatDeserializer(Float.class, null),
new StdDeserializer.DoubleDeserializer(Double.class, null),
new StdDeserializer.NumberDeserializer(),
new StdDeserializer.BigDecimalDeserializer(),
new StdDeserializer.BigIntegerDeserializer(),
new StdDeserializer.CalendarDeserializer()
}));
ObjectMapper om = factory.createObjectMapper();
You might want to let your controller handle the problem by adding a method that handles this specific exception
#ExceptionHandler(HttpMessageNotReadableException.class)
#ResponseBody
public String handleHttpMessageNotReadableException(HttpMessageNotReadableException ex)
{
JsonMappingException jme = (JsonMappingException) ex.getCause();
return jme.getPath().get(0).getFieldName() + " invalid";
}
Of course, the line
JsonMappingException jme = (JsonMappingException) ex.getCause();
might throw a class cast exception for some cases but i haven't encountered them yet.
I have written a simple error handler which will give you some kind of error which you can return to user with bad request as status code. Use #JsonProperty required = true to get error related to missing properties. Jackson version 2.9.8.
public class JacksonExceptionHandler {
public String getErrorMessage(HttpMessageNotReadableException e) {
String message = null;
boolean handled = false;
Throwable cause = e.getRootCause();
if (cause instanceof UnrecognizedPropertyException) {
UnrecognizedPropertyException exception = (UnrecognizedPropertyException) cause;
message = handleUnrecognizedPropertyException(exception);
handled = true;
}
if (cause instanceof InvalidFormatException) {
InvalidFormatException exception = (InvalidFormatException) cause;
message = handleInvalidFormatException(exception);
handled = true;
}
if (cause instanceof MismatchedInputException) {
if (!handled) {
MismatchedInputException exception = (MismatchedInputException) cause;
message = handleMisMatchInputException(exception);
}
}
if (cause instanceof JsonParseException) {
message = "Malformed json";
}
return message;
}
private String handleInvalidFormatException(InvalidFormatException exception) {
String reference = null;
if (!exception.getPath().isEmpty()) {
String path = extractPropertyReference(exception.getPath());
reference = removeLastCharacter(path);
}
Object value = exception.getValue();
return "Invalid value '" + value + "' for property : " + reference;
}
private String handleUnrecognizedPropertyException(UnrecognizedPropertyException exception) {
String reference = null;
if (!exception.getPath().isEmpty()) {
String path = extractPropertyReference(exception.getPath());
reference = removeLastCharacter(path);
}
return "Unknown property : '" + reference + "'";
}
private String handleMisMatchInputException(MismatchedInputException exception) {
String reference = null;
if (!exception.getPath().isEmpty()) {
reference = extractPropertyReference(exception.getPath());
}
String property = StringUtils.substringBetween(exception.getLocalizedMessage(), "'", "'");
// if property missing inside nested object
if (reference != null && property!=null) {
return "Missing property : '" + reference + property + "'";
}
// if invalid value given to array
if(property==null){
return "Invalid values at : '"+ reference +"'";
}
// if property missing at root level
else return "Missing property : '" + property + "'";
}
// extract nested object name for which property is missing
private String extractPropertyReference(List<JsonMappingException.Reference> path) {
StringBuilder stringBuilder = new StringBuilder();
path.forEach(reference -> {
if(reference.getFieldName() != null) {
stringBuilder.append(reference.getFieldName()).append(".");
// if field is null means it is array
} else stringBuilder.append("[].");
}
);
return stringBuilder.toString();
}
// remove '.' at the end of property path reference
private String removeLastCharacter(String string) {
return string.substring(0, string.length() - 1);
}
}
and call this class object in global advice like this
#Override
protected ResponseEntity<Object> handleHttpMessageNotReadable(HttpMessageNotReadableException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
String message = new JacksonExceptionHandler().generator.getErrorMessage(ex);
if(message == null){
return ResponseEntity.badRequest().body("Malformed json");
}
return ResponseEntity.badRequest().body(message);
}
Create a simple Mapper:
#Provider
#Produces(MediaType.APPLICATION_JSON)
public class JSONProcessingErroMapper implements ExceptionMapper<InvalidFormatException> {
#Override
public Response toResponse(InvalidFormatException ex) {
return Response.status(400)
.entity(new ClientError("[User friendly message]"))
.type(MediaType.APPLICATION_JSON)
.build();
}
}
DeserializationProblemHandler now has a lot more methods, such as handleUnexpectedToken and handleWeird*Value. It should be able to handle anything one needs.
Subclass it, override methods you need, then add it to your ObjectMapper with addHandler(DeserializationProblemHandler h).

Categories