Jersey ExceptionMapper doesn't map exceptions

Related searches

my Spring Boot + Jersey REST service doesn't work as expected.

EmailExistsException is thrown in UserController but I only receive error 500. All the time. And my exceptions aren't logged.

I suspect there is some configuration issue with exception handling but don't know where to set it up. Any suggestions?

@POST
@Path("register")
@Produces(MediaType.APPLICATION_JSON)
public Response register(NewUserPayload newUserPayload) throws EmailExistsException, MessagingException

EmailExistsExceptionMapper

@Provider
public class EmailExistsExceptionMapper extends AbstractExceptionMapper       implements
    ExceptionMapper<EmailExistsException>
{
@Override
public Response toResponse(EmailExistsException e)
{
    ResponseEntity re = new ResponseEntity(org.springframework.http.HttpStatus.BAD_REQUEST);

    return this.errorResponse(HttpStatus.BAD_REQUEST_400, re, e);
}
}

AbstractExceptionMapper

@Slf4j
public abstract class AbstractExceptionMapper
{
protected Response errorResponse(int status, ResponseEntity responseEntity, Throwable t)
{
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    t.printStackTrace(pw);
    log.error(sw.toString()); // logging stack trace.

    return customizeResponse(status, responseEntity);
}

private Response customizeResponse(int status, ResponseEntity responseEntity)
{
    return Response.status(status).entity(responseEntity).build();
}
}

build.gradle

compile("org.springframework.boot:spring-boot-starter-web") {
    exclude module: 'spring-boot-starter-tomcat'
}
compile "org.springframework.boot:spring-boot-starter-jetty"
compile "org.springframework.boot:spring-boot-starter-security"
compile "org.springframework.boot:spring-boot-starter-aop"
compile "org.springframework.boot:spring-boot-starter-data-jpa"
compile "org.springframework.boot:spring-boot-starter-thymeleaf"
compile "org.springframework.boot:spring-boot-starter-jersey"
compile 'org.springframework.boot:spring-boot-starter-mail'

Answer that solved my problems:

I've put packages("package.name"); which is my root package name and exception mappers work like a charm.

@Component
@ApplicationPath("/api")
public class JerseyConfig extends ResourceConfig
{
public JerseyConfig()
{
    packages("package.name");
    register(UserController.class);
    register(TimezoneController.class);
}
}

Jersey ExceptionMapper doesn't map exceptions, Answer that solved my problems: I've put packages("package.name"); which is my root package name and exception mappers work like a� On Sat, Jun 18, 2011 at 2:28 PM, <[hidden email]> wrote: I created a small ExceptionMapper that I thought would catch all exceptions thrown from my resource class. But it doesn't catch any

Have you configured custom ExceptionMapper as a jax-rs provider, and Are you sure that your exception is getting wrapped into EmailExistsException? You may have to look at this post.

JAX-RS using exception mappers

Jersey ExceptionMapper not working with spring boot?, I am using jersey with spring boot, and I have a exception mapper: package UNAUTHORIZED; @Provider public class ExceptionMapper implements javax. ws.rs.ext. Possible duplicate of Jersey ExceptionMapper doesn't map exceptions� is no longer called - Jersey uses the built-in exception mapper for this exception: org.glassfish.jersey.server.validation.internal.ValidationExceptionMapper and unfortunately I cannot override it. The problem is strictly related to the Weld integration.

I had the same problem

I just mentioned the root package on the web.xml file in <param-value> tag of <init-param>

Then It started working like charm

<servlet>
    <servlet-name>Jersey Web Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>com.two95.restful</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

Jersey ExceptionMapper doesn't map exceptions - spring - html, my Spring Boot + Jersey REST service doesn't work as expected. EmailExistsException is thrown in UserController but I only receive error 500. All the time. What’s interesting about the default error handling for JAX-RS is that you can write an ExceptionMapper for these scenarios. For example, if you want to send back a different response to the client when JAX-RS cannot find an @Produces match for an Accept header, you can write an ExceptionMapper for NotAcceptableException. This gives you complete control on how errors are handled by your application.

If you using ExceptionMapper, you must register your exception mapper:

@Component
@Provider
public class ApiApplicationExceptionMapper implements ExceptionMapper<Exception> {

    @Override
    public Response toResponse(Exception exception) {
       ...
    }
}

In Jersey config class, scan Jersey Endpoint class with @Path annotation and custom Exception Mapper with @Provider annotation to register:

@Configuration
public class JerseyConfig extends ResourceConfig {

    @Autowired
    ApplicationContext applicationContext;

    @PostConstruct
    public void init() {
        this.registerEndpoints();
        this.registerExceptionMappers();
    }

    private void registerEndpoints() {
        Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Path.class);
        for (Object apiClass : beans.values()) {
            logger.info("Jersey register: " + apiClass.getClass().getName());
            register(apiClass);
        }
    }

    private void registerExceptionMappers() {
        Map<String, Object> beans = applicationContext.getBeansWithAnnotation(Provider.class);
        for (Object exceptionMapper : beans.values()) {
            logger.info("Jersey exception mapper register: " + exceptionMapper.getClass().getName());
            register(exceptionMapper);
        }
    }

ExceptionMapper (Java(TM) EE 7 Specification APIs), E - exception type supported by the provider. public interface ExceptionMapper< E extends Throwable>. Contract for a provider that maps Java exceptions to� Generic Jersey Error Handling We create a GenericExceptionMapper that implements the ExceptionMapper interface. This implies us to override the toResponse () method. In this method we can create a response object that’ll be triggered when an exception occurs.

Custom ExceptionMappers not working properly. (dropwizard , And then registered the exception mapper with jersey. @nickbabcock I have tried all of that and it doesn't work, if we remove the entity from� In this article, we've introduced the Jersey framework and developed a simple API. We've used Spring for Dependency Injection features. We have also seen the use of ExceptionMapper. As always, the full source code is available in this Github project.

Now, when JAX-RS sees any INFONotFoundException in the application, it searches all ExceptionMapper ‘s annotated with @Provider in application and tries to find a mapper which could map the INFONotFoundException to a ErrorResponse.

Nothing is logged by Jersey or WebLogic either - since the exception mapper implementation doesn't do any logging. That first implementation is a step forward but we can do better. Instead of relying on Jersey to generate output from the String entity we can use a message body writer to convert HelloException to data on the Response output stream.

Comments
  • no information about this configuration in the jersey documentation. Does the package declaration will avoid you to declare controllers ?
  • I've found it in some code snippet on the internet. I'm in Scala now so couldn't tell you more info about it.
  • Thanks! It turns out you have to either explicitly register(YourExceptionMapper.class) or place it in a scanned package via packages("YourExceptionMapperPackage"). I spent couple of hours trying to figure this out before I found your answer.
  • It has a @Provider annotation and yes, I'm sure that it gets wrapped and thrown. Another thing is that when I try to put breakpoint in EmailExistsExceptionMapper it is not alive in IDEA in debug mode. It seems that somehow it isn't used? Some config is missing maybe but where?