Jersey ExceptionMapper不映射异常

时间:2015-01-16 11:25:44

标签: spring web-services rest jersey spring-boot

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

在UserController中抛出EmailExistsException,但我只收到错误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);
}
}

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'

4 个答案:

答案 0 :(得分:17)

解决我的问题的答案:

我放了包(&#34; package.name&#34;);这是我的根包名称,异常映射器就像魅力一样。

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

答案 1 :(得分:1)

您是否已将自定义ExceptionMapper配置为jax-rs提供程序,并确定您的异常包含在EmailExistsException中吗?你可能要查看这篇文章。

JAX-RS using exception mappers

答案 2 :(得分:0)

我遇到了同样的问题

我刚才在<param-value>的{​​{1}}标记中提到了web.xml文件中的root包

然后它开始像魅力一样工作

<init-param>

答案 3 :(得分:0)

如果使用ExceptionMapper,则必须注册异常映射器:

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

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

在Jersey配置类中,扫描带有@Path批注的Jersey端点类和带有@Provider批注的自定义异常映射器以注册:

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