1

I am working on JAX-RS API, where user sending below JSON payload to my API:

{
    "text": "test search",
    "count": "myvalue"
}

As mentioned above, count is of type Integer in POJO SearchDetailsInfo.java, but user sending some garbage string in it while posting data to this API.

Please find my controller method below:

@POST
@Path("/myview")
@Produces(MediaType.APPLICATION_JSON)
public OrderedJSONObject getCatalogView(SearchDetailsInfo criteria,
                @Context ContainerRequestContext containerRequestContext) {
        .... processing ....
}

As, there is data type mismatch API getting response error as below:

Status Code : 400
Status Message : Bad Request
Body:
    Can not construct instance of java.lang.Integer from String value 'myvalue': 
    not a valid Integer value at [Source: org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$UnCloseableInputStream@bd8b3db; line: 5, column: 15] 

As there is datatype mismatch, Jersey throwing invalid datatype error.

Unfortunately, I am not able to catch this exception because request is not coming inside controller method due to exception in datatype parsing.

Would like to know, How can I catch this exception so that I could change error response to something meaningful.

Thanks.

Joe
  • 157
  • 1
  • 12
  • What JSON provider are you using? – Paul Samsotha Sep 06 '18 at 18:44
  • I am using `jersey-media-json-jackson` as its a jersey project – Joe Sep 07 '18 at 05:49
  • You can create an `ExceptionMapper` and `ExceptionMapper` (yes, both) and register them. Return whatever you want in those mappers. – Paul Samsotha Sep 07 '18 at 06:00
  • See [the Jackson one](https://github.com/FasterXML/jackson-jaxrs-providers/blob/9958c0347e90f6f6509fca36f4731da006c2a86b/base/src/main/java/com/fasterxml/jackson/jaxrs/base/JsonMappingExceptionMapper.java), all they do is return the exception message. That's what you're currently seeing. – Paul Samsotha Sep 07 '18 at 06:01
  • Hi Paul, thanks for response. Creating class `JsonMappingExceptionMapper` and extend to `ExceptionMapper` will work ? or Do I need to hook into application as well? – Joe Sep 07 '18 at 08:30
  • You need to register then with Jersey. – Paul Samsotha Sep 07 '18 at 09:05
  • could you please suggest how can I register it with jersey ? Could you please share sample for this ? – Joe Sep 07 '18 at 09:56
  • Read [this article](https://blog.dejavu.sk/2013/11/19/registering-resources-and-providers-in-jersey-2/). ExceptionMappers are providers. – Paul Samsotha Sep 07 '18 at 18:43
  • This [answer](https://stackoverflow.com/a/50333717/1426227) may be helpful. Don't forget to register your provider with a priority (otherwise it may not be picked up). – cassiomolin Sep 11 '18 at 13:15

1 Answers1

1

You can handle this by implementing error mapper. For the example you shown above, it looks like Exception is getting thrown at internal processing while mapping JSON Data to POJO.

If you look at logs closely, you will find errors like InvalidFormatException or JsonMappingException while processing data.

You can create Exception Mapper for the error you are getting. I would recommend to use super JsonMappingException as it will take care of error like invalid type, invalid JSON in request payload, etc:

@Provider
public class GenericExceptionMapper extends Throwable implements ExceptionMapper<JsonMappingException> {
    @Override
    public Response toResponse(JsonMappingException thrExe) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("errorMessage", "Invalid input provided");
        return Response.status(400).entity(jsonObject.toString())
        .type("application/json").build();
    }
}
NikhilK
  • 181
  • 2
  • 12