Jersey ExceptionMapper 不映射异常 [英] Jersey ExceptionMapper doesn't map exceptions

查看:32
本文介绍了Jersey ExceptionMapper 不映射异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Spring Boot + Jersey REST 服务没有按预期工作.

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

在 UserController 中引发了 EmailExistsException,但我只收到错误 500.一直.而且我的异常没有被记录.

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

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

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

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:

我已经放了 packages("package.name");这是我的根包名,异常映射器就像一个魅力.

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 不映射异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆