i've a Rest built using jersey and i try to handle exceptions. I registered jackson how default json processor for all operations regarding json mapping using this class:
@javax.ws.rs.ApplicationPath("/")
public class MyApplication extends ResourceConfig {
public MyApplication() {
packages("controller");
register(JacksonFeature.class);
}
}
I defined an exception mapper to catch some exceptions like for example "UnauthorizedException" and "BadRequestException" using the following class:
@Provider
public class ClientExceptionMapper implements ExceptionMapper<BaseException>
{
@Override
public Response toResponse(BaseException ex)
{
System.out.println("qwertweryt");
if(ex instanceof UnauthorizedException)
{
System.out.println("Unauthorized!");
return (((UnauthorizedException)ex).getResponse());
}
else if(ex instanceof BadrequestException)
{
System.out.println("Badrequest!");
return (((BadrequestException)ex).getResponse());
}
return null;
}
}
where BaseException, UnauthorizedException and BarequestException are respectively:
public abstract class BaseException extends WebApplicationException
{
private Response r;
public BaseException(Response response)
{
r = response;
}
public Response getResponse()
{
return r;
}
}
public class UnauthorizedException extends BaseException
{
public UnauthorizedException(Response response)
{
super(response);
}
}
public class BadrequestException extends BaseException
{
public BadrequestException(Response response)
{
super(response);
}
}
So, all works fine until i handle jackson exceptions...In fact i defined another exception mapper ,similar to the first one, to handle all the exception generated by jackson (like for example JsonMappingException) but doesn't work. I report you the error i obtain by my application:
org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "serverr" (Class controller.server.control.response.GetServerDetailsResponse), not marked as ignorable
at [Source: org.glassfish.jersey.message.internal.EntityInputStream@701d7463; line: 1, column: 14] (through reference chain: controller.server.control.response.GetServerDetailsResponse["serverr"])
at org.codehaus.jackson.map.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java:53)
at org.codehaus.jackson.map.deser.StdDeserializationContext.unknownFieldException(StdDeserializationContext.java:267)
at org.codehaus.jackson.map.deser.std.StdDeserializer.reportUnknownProperty(StdDeserializer.java:673)
at org.codehaus.jackson.map.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:659)
at org.codehaus.jackson.map.deser.BeanDeserializer.handleUnknownProperty(BeanDeserializer.java:1365)
at org.codehaus.jackson.map.deser.BeanDeserializer._handleUnknown(BeanDeserializer.java:725)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:703)
at org.codehaus.jackson.map.deser.BeanDeserializer.deserialize(BeanDeserializer.java:580)
at org.codehaus.jackson.map.ObjectMapper._readValue(ObjectMapper.java:2704)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1315)
at org.codehaus.jackson.jaxrs.JacksonJsonProvider.readFrom(JacksonJsonProvider.java:419)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:181)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:134)
at org.glassfish.jersey.server.internal.MappableExceptionWrapperInterceptor.aroundReadFrom(MappableExceptionWrapperInterceptor.java:72)
at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:134)
My first question is: it may be that the two classes go in conflict, that is there aren't two exception mappers in the same project? The second question is: how can i resolve this issue? I hope i was clear and thanks you in advance for your help!
Hi, i just read a post in which it's presented the same problem that i have; i report you the link of the question: Jersey: Returning 400 error instead of 500 when given invalid request body . As first thing i try the code written in the first answer of the post but with the same result as before: i always obtain 500 error code instead of 400 when i knowingly insert an error in the request body. I also try to put @JsonIgnoreProperties(ignoreUnknown=true) above the class that deals with intercept the request body, but doesn't work for me.