Override response when property in JSON is missing (Jackson 2.7.0+)

578 views
Skip to first unread message

t7a...@gmail.com

unread,
Jan 27, 2016, 1:13:35 PM1/27/16
to jackson-user
Hi!

My entity look like

public class Consumption{

private int id;
private double quantity;
private double distance;

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES)
public Consumption(@JsonProperty(value = "quantity", required = true)double quantity, 
@JsonProperty(value = "distance", required = true)double distance,
@JsonProperty(value = "vehicle", required = false)Vehicle vehicle) {...

And REST method:
@POST
@Path("/setConsumption/")
public Response setConsumption(@Valid Consumption consum){...

When I receive JSON like below, everything is ok:
{"vehicle":{"id":1},"distance":1000,"quantity":2000}
 

But when i receive JSON like:
{"vehicle":{"id":1},"quantity":2000}
 

I get exception in JSON:
Missing required creator property 'distance' (index 1) at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@1b72cb6; line: 1, column: 181]

Is there a way how to  override this exception message and return it as custom JSON format, which could client parser understand?
As I have been testing, validation does not reach my REST POST "setConsumption/" method.

Thanks.
Have a nice day, T.

Tatu Saloranta

unread,
Jan 27, 2016, 2:03:37 PM1/27/16
to jackso...@googlegroups.com
When passing properties via Constructor, setter is never called: data is only passed to you.
But if you specify something `required`, and there is no value, an exception is called before constructor gets a chance to be called. So at that point, Jersey would be responsible for catching the exception.

You may want to just remove `required=true` setting, if you want to handle this yourself. Or alternatively figure out how you can catch and handle exception via Jersey's exception handler system. I think this is done quite often, to massage errors to be bit more user-friendly.

-+ Tatu +-


--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

t7a...@gmail.com

unread,
Jan 29, 2016, 12:06:24 PM1/29/16
to jackson-user
Someone on StackOverflow suggested to create my own ExceptionMapper with JsonMapping Exception.
I have found similar solutions and examples then, but as many others my mapper does not get registered.

My mapper:
@Provider
public class JacksonNoPropertyMapper implements ExceptionMapper<JsonMappingException> {//JsonMappingException

public JacksonNoPropertyMapper() {
System.out.println("TEST mapper created");
}

@Override
public Response toResponse(JsonMappingException e) {
return Response.status(999).entity("MY MAPPING EXCEPTION").type(MediaType.APPLICATION_JSON).build();
}

Also some people suggests to disable JacksonFeature and register my own ExceptionMapper, but i don't in which class to do this.

Tatu Saloranta

unread,
Jan 29, 2016, 1:25:49 PM1/29/16
to jackson-user
There isn't much Jackson can do here, so DropWizard mailing list (or, if not DW, specific framework or basic Jersey?) might be able to give more help.

But perhaps someone on this list can share their setup.

-+ Tatu +-


Reply all
Reply to author
Forward
0 new messages