ExceptionMapper is not called when receiving invalid JSon

I am using Jersey 2.5.1 with Jackson 2.2. to create JSON Rest web services. I kind of started and ran 2 ExceptionMappers as well, but the application does not throw any exceptions for the non-json request!

  • ExceptionMapper Will be raised if, for example, a NullPointerException

  • ExceptionMapper Will be thrown if there is a problem with JSon Mapping

My problem: 1. The request body: {} works 2. The request body: {} with the NullPointer application side called the first exception display unit 3. The request body: "jibberish" does not cause anything (not caught by any ExceptionMapper), because the Exception is not thrown away. Unfortunately, the response body is similar to: the unrecognized field "xxx" (class com.sample.MyDto), not marked as ignorant (9 known properties ... ....> but I want to configure the msg error, since I always return JSon object.

0
source share
2 answers

, . , , , , , .

ApplicationClass ,

    @ApplicationPath("resources")
    public class JerseyApplication extends ResourceConfig {
    public JerseyRestApplication() {            
        packages("com.sample", "com.fasterxml.jackson.jaxrs.json");
    }
}

, ! "com.fasterxml.jackson.jaxrs.json" , JacksonJsonProvider.class. , JSonMappers JSonParseException .., :)

, . :

@ApplicationPath("resources")
public class JerseyRestApplication extends ResourceConfig {
    public JerseyApplication() {
        register(JacksonJsonProvider.class); // Do it manually

        packages("com.sample");
    }
}

, sb: D

+1

, - coustom ExceptionMapper , .

.

class: org.glassfish.jersey.server.ServerRuntime, methed: mapException

 ...
 final long timestamp = tracingLogger.timestamp(ServerTraceEvent.EXCEPTION_MAPPING);
 **ExceptionMapper mapper = runtime.exceptionMappers.findMapping(throwable);**
 if (mapper != null) {
      request.getRequestEventBuilder().setExceptionMapper(mapper);
 ...

null, ExceptionMapper .

class: org.glassfish.jersey.internal.ExceptionMapperFactory methed: ExceptionMapperFactory

Exception Mapper: ( Exception : java.lang.Exception)

org.glassfish.jersey.server.mvc.internal.ErrorTemplateExceptionMapper @6473fc2, java.lang.Exception

...

com.baidu.ssp.web.ws.exception.BaseExceptionMapper @7a84639c, java.lang.Exception

MvcFeature:

 @Override
public boolean configure(final FeatureContext context) {
    final Configuration config = context.getConfiguration();

    if (!config.isRegistered(ErrorTemplateExceptionMapper.class)) {
        context.register(ErrorTemplateExceptionMapper.class);
        context.register(new MvcBinder());

        return true;
    }

    return false;
}

ErrorTemplateExceptionMapper ExceptionMapper.

, MapperException: ExceptionMapper ExceptionMapper

, ExceptionMapper.

0

Source: https://habr.com/ru/post/1621624/


All Articles