ExceptionMapper not invoked if receiving invalid JSon

穿精又带淫゛_ 提交于 2020-01-03 04:51:27

问题


I am using Jersey 2.5.1 with Jackson 2.2. for building JSON Rest web services. I kind of got it up and running also with 2 ExceptionMappers but for a "not-json" request the application is not throwing any exception!

  1. ExceptionMapper Will be invoked if e.g. a NullPointerException is thrown

  2. ExceptionMapper Will be invoked if there s a problem with the JSon Mapping

My Problem: 1. Request Body: {} works 2. Request Body: {} with an application side NullPointer invoked the first exception mapper 3. Request Body: "jibberish" does not invoke anything (not caught by any ExceptionMapper) cause no Exception is thrown. Unfortunately the response body is sth like : Unrecognized field "xxx" (class com.sample.MyDto), not marked as ignorable (9 known properties... ....> but I want to customize the error msg since I am always returning a JSon object.


回答1:


Ok, I solved it. What finally made me think the right way was, that sometimes it worked so it had to be some overwrite class loading issue.

In my ApplicationClass I registered Providers based on packages like this

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

If you look sharp you might see the error! I am putting "com.fasterxml.jackson.jaxrs.json" on the search path because I want to use the JacksonJsonProvider.class. What I did not know is, that in that package there are also some JSonMappers for JSonParseException etc. and those do not log :)

So sometimes the application was loading mine first and sometimes the one from jackson. So I am going to stick with the following now:

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

        packages("com.sample");
    }
}

Hope that helps sb :D




回答2:


I also found sometime coustom ExceptionMapper not work, and it's not always work or not work.

so i debug the jersey's source code .

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);
 ...

if the mapper is null, the coustom ExceptionMapper will be not work.

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

the Exception Mapper :(there two Exception mapping one same Exception: java.lang.Exception)

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

...

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

it's because in 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;
}

the ErrorTemplateExceptionMapper is also add to ExceptionMapper.

so i change my custom MapperException's genericity type : ExceptionMapper to ExceptionMapper

may be my resolution is not fit for you , the main problem is the ExceptionMapper.



来源:https://stackoverflow.com/questions/21312121/exceptionmapper-not-invoked-if-receiving-invalid-json

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!