Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jersey ExceptionMapper doesn't map exceptions

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'
like image 697
Reeebuuk Avatar asked Jan 16 '15 11:01

Reeebuuk


People also ask

What is @exceptionmapper?

ExceptionMapper is a contract for a provider that maps Java exceptions to Response object. An implementation of ExceptionMapper interface must be annotated with @Provider to work correctly. 1. Resteasy ExceptionMapper – Custom exception handler A sample implementation provider class of ExceptionMapper looks like this:

How to map an exception to a mapper in JAX-RS?

To map an exception to a mapper, JAX-RS runtime implementation, selects the mapper whose generic type is the nearest superclass of the exception. Using HTTPie to access the resource: Dependencies and Technologies Used: jersey-server 2.25.1: Jersey core server implementation. jersey-container-servlet 2.25.1: Jersey core Servlet 3.x implementation.

When does Jersey use the illegalargumentexceptionmapper?

Then, depending on what is thrown, Jersey will use one of our exception mappers when an unhandled exception occurs. For instance, when trying to get a Stock that doesn't exist, the IllegalArgumentExceptionMapper will be used: Likewise, for other unhandled exceptions, the broader ServerExceptionMapper will be used.

How to throw a missingfileexception in Jersey exceptionmapper?

Now you can throw MissingFileException when ever user requested file is not found in desired location. throw new MissingFileException (fileName + " does not existing on this server !!"); throw new IOException ("Error while reading file :: '"+fileName+"' !!"); 3. Jersey exception handling example Now is the time to test jersey exceptionmapper.


2 Answers

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);
}
}
like image 194
Reeebuuk Avatar answered Sep 18 '22 01:09

Reeebuuk


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

like image 34
Lovababu Avatar answered Sep 17 '22 01:09

Lovababu