spring - Jersey ExceptionMapper 不映射异常

标签 spring web-services rest jersey spring-boot

我的 Spring Boot + Jersey REST 服务无法按预期工作。

EmailExistsException 在 UserController 中抛出,但我只收到错误 500。一直都是。而且我的异常没有被记录。

我怀疑异常处理存在一些配置问题,但不知道在哪里设置。有什么建议吗?

@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);
}
}

抽象异常映射器

@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();
}
}

构建.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'

最佳答案

解决我的问题的答案:

我已经把packages("package.name");这是我的根包名称,异常映射器工作起来就像一个魅力。

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

关于spring - Jersey ExceptionMapper 不映射异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27982948/

相关文章:

java - Spring Cloud Config Encryption API 忽略最后的特殊字符

java - 如何在@GET中添加到Web服务迭代元素?

web-services - Windows Phone 7 - 加速数据获取的最佳实践

java - RESTful 服务方法可以向客户端发送对象吗?

java - 如何在 Spring 3 中上传图像并将 JSON 发布到 RESTful Web 服务

java - 使用在规范 (JAX-RS) 中定义或在实现 (Jersey) 中定义的类?

java - Spring SimpleFormController onSubmit请求参数

java - 类级别验证问题

android - 为什么在 Android 中使用 sqlite 数据库?

java - Spring MVC 和 Struts MVC 的区别