ExceptionMapper无法按预期工作 [英] ExceptionMapper not working as expected

查看:142
本文介绍了ExceptionMapper无法按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将jersey用于REST Web服务.

I am using jersey for REST web services.

每当我没有从服务层获取任何对象时,我就会通过抛出NotFoundException(Package com.sun.jersey.api)处理所有404响应.

I am handling all 404 responses by throwing NotFoundException(Package com.sun.jersey.api) whenever I don't get any object from service layer.

例如

@GET
@Path("/{folderID}")
@Produces(MediaType.APPLICATION_JSON)
public Response getFolder(@PathParam("folderID") int folderID) {
       .
       .
       .
       Folder folderObj = folderService.getFolder(folderID);
       if(folderObj == null){
            throw new NotFoundException("Folder with ID '"+folderID+"'  not found.");
       }
}

我已经为此异常编写了ExceptionMapper.

I have written ExceptionMapper for this exception.

@Provider
public class NotFoundExceptionMapper implements   ExceptionMapper<NotFoundException> {

public Response toResponse(NotFoundException ex) {
    ErrorMesage errorMessage = new ErrorMessage();
    errorMessage.setCode(Status.NOT_FOUND.getStatusCode());
    errorMessage.setMessage(ex.getMessage());

    return Response.status(Status.NOT_FOUND)
            .entity(errorMessage)
            .type(MediaType.APPLICATION_JSON)
            .build();
}

}

因此,当我给未知的文件夹ID作为路径参数时,会引发异常,但不会调用NotFoundExceptionMapper中的代码. (我可以在响应中看到异常消息,但显示为纯文本",即使在mapper中我以JSON返回响应;也没有命中调试断点).

So When I give unknown folder ID as path parameter, exception is thrown but code in NotFoundExceptionMapper is not invoked. (I can see exception message in response but as 'plain text', even though in mapper I am returning response in JSON; and debug break point is also not hit).

此外,当我在URI中输入错误的资源名称时,会调用上述异常映射器,但对于错误的路径参数则不会.

Also, Above exception mapper is invoked when I enter incorrect resource name in URI, but not for incorrect path param.

我还像下面一样添加了异常映射器,以响应所有其他异常.

I have also added exception mapper like below to respond to all other exceptions.

public class GenericExceptionMapper implements  ExceptionMapper<Throwable>{

public Response toResponse(Throwable ex) {
    ErrorMessage errorMessage = new ErrorMessage();
    errorMessage.setCode(Status.INTERNAL_SERVER_ERROR.getStatusCode());
    errorMessage.setMessage(ex.getMessage());


    return Response.status(errorMessage.getCode())
            .entity(errorMessage)
            .type(MediaType.APPLICATION_JSON)
            .build();   
}

每当引发任何异常(映射异常除外)并且我得到正确的JSON响应时,就会调用上述代码.

Above code is called whenever any exception (other than mapped exceptions) is thrown and I get proper JSON response.

那么这里的NotFoundException有什么问题? 我已经在Google上进行了搜索,还调查了NotFoundException的来源,但没有发现任何有用的信息,请指导我.

So what is wrong with NotFoundException here? I have googled about this and also looked into source of NotFoundException but didn't find anything useful, please guide me on this.

推荐答案

球衣ServerRuntime类的摘录.它具有特殊的逻辑,如果ExceptionWebApplicationException的实例并且具有主体,则根本不会进入异常映射器.

A snippet from the jersey ServerRuntime class. It has a special logic that if the Exception is an instance of WebApplicationException and has a body, it does not go to the exception mappers at all.

private Response mapException(Throwable originalThrowable) throws Throwable {
    if (throwable instanceof WebApplicationException) {
        WebApplicationException webApplicationException = (WebApplicationException)throwable;
     }

    this.processingContext.routingContext().setMappedThrowable(throwable);
    waeResponse = webApplicationException.getResponse();
    if (waeResponse.hasEntity()) {
        LOGGER.log(java.util.logging.Level.FINE, LocalizationMessages.EXCEPTION_MAPPING_WAE_ENTITY(waeResponse.getStatus()), throwable);
        return waeResponse;
    }        

    long timestamp = this.tracingLogger.timestamp(ServerTraceEvent.EXCEPTION_MAPPING);
    ExceptionMapper mapper = this.runtime.exceptionMappers.findMapping(throwable);
}

这篇关于ExceptionMapper无法按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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