JAX-RS Custom ExceptionMapper Does Not Catch RuntimeException

I want to subclass RuntimeExceptionsa custom json format by making the servlet container without dropping stacktrace to the client.

I follow this question: JAX-RS (Jersey) custom exception with XML or JSON . When called:

try {
  doSomething(parameters);
}
catch(RuntimeException e) {
  throw new MyCustomException(500 , e.getMessage() , Status.INTERNAL_SERVER_ERROR);
}

When I intentionally load the wrong parameters (and the trigger RuntimeExceptionthrown doSomething()), I did not see MyCustomExceptionMapper. Instead, servlet containers are unloaded:

The RuntimeException could not be mapped to a response, re-throwing to the HTTP container
api.MyCustomException: (underlaying msgs)

MyCustomExceptionMapperreally registered in javax.ws.rs.core.Application:

  @Override
  public Set<Class<?>> getClasses()
  {
    Set<Class<?>> set = new HashSet<Class<?>>();
    set.add(other classes);
    set.add(MyCustomExceptionMapper.class);
    return set;
  }

What did I miss?

Thank you so much!

Environment: JAX-RS, jersey-server 1.5

class spec:

class MyCustomException extends RuntimeException 
@Provider
class MyCustomExceptionMapper implements ExceptionMapper<MyCustomException>

updated :

I suspect it is Application.getClasses()never called, so I am adding a few println messages:

  @Override
  public Set<Class<?>> getClasses()
  {
    System.out.println("\n\n\n\n ApiConfig getClasses");
  }

!

, ApiConfig web.xml:

  <context-param>
    <param-name>javax.ws.rs.core.Application</param-name>
    <param-value>destiny.web.api.ApiConfig</param-value>
  </context-param>

?

+5
5

.

, , MyCustomExceptionMapper Spring @Repository.

web.xml( )

  <context-param>
    <param-name>javax.ws.rs.core.Application</param-name>
    <param-value>destiny.web.api.ApiConfig</param-value>
  </context-param>

Spring @Repository @Provider, .

+2

( ), , , , RuntimeException, , raw RuntimeException; , . , (, , , AOP), .

, . , Apache CXF ( JAX-RS, ) / , @Provider Spring config, ...

+1

web.xml web.xml, .

- javax.ws.rs.Application( javax.ws.rs.core.Application, , ).

. : docs.oracle.com/cd/E24329_01/web.1211/e24983/configure.htm#RESTF179

+1

web.xml. @Repository ExceptionMapper.

<servlet>
    <servlet-name>rest-servlet</servlet-name>
    <servlet-class>com.sun.jersey.spi.spring.container.servlet.SpringServlet</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>your.base.package.to.rest</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>rest-servlet</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

:

INFO: Scanning for root resource and provider classes in the packages:
your.base.package.to.rest

INFO: Root resource classes found:
class your.base.package.to.rest.resources.FooResource

INFO: Provider classes found:
class your.base.package.to.rest.providers.NotFoundMapper

  • v1.11 12.09.2011 10:27
  • Spring v3.1.1
  • GlassFish Server Open Source Edition 3.1.2 ( 23)
0

I had the same problem and a change in web.xml, in particular in the tag, solved the problem. Just make sure that include includes your package for exception and exception classes, not just packages containing models and resources jersey.config.server.provider.packages basepackage   

ex. if the package for models / objects is pkg.entity, and for the exception, pkg.exception param-value (basepackage) will be pkg. If basepackage is installed by pkg.entity, it does not work ... this is how I solved the problem.

0
source

All Articles