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.