38

I am new to this, trying to achieve reading some docs but its not working, please bear with me.

I have created a UserNotFoundMapper using ExceptionMappers like this:

public class UserNotFoundMapper implements ExceptionMapper<UserNotFoundException> {

@Override
public Response toResponse(UserNotFoundException ex) {
    return Response.status(404).entity(ex.getMessage()).type("text/plain").build();
}

}

This in my service:

@GET
@Path("/user")
public Response getUser(@QueryParam("id") String id) throws UserNotFoundException{
    //Some user validation code with DB hit, if not found then
    throw new UserNotFoundException();
}

The UserNotFoundException is an User-Defined Exception.

I tried this:

public class UserNotFoundException extends Exception {
       //SOME block of code 
}

But when I invoke the service, the UserDefinedExceptionMapper is not getting invoked. It seems I might be missing something in the UserDefinedException. How to define this exception then?

Please let me know how to define the UserNotFoundException.

4

3 回答 3

56

您需要使用 注释您的异常映射器@Provider,否则它将永远不会注册到 JAX-RS 运行时。

@Provider
public class UserNotFoundMapper implements
        ExceptionMapper<UserNotFoundException> {
    @Override
    public Response toResponse(UserNotFoundException ex) {
        return Response.status(404).entity(ex.getMessage()).type("text/plain")
                .build();
    }
}
于 2013-03-03T12:25:37.763 回答
3

创建 API 时我通常做的是创建我自己的从RuntimeException扩展的异常,因此我不必捕获我的异常。

这是一个例子:

注意:我将 JAX-RS 与泽西岛一起使用

首先:创建我自己的从 RuntimeException 扩展的异常。

public class ExceptionName extends RuntimeException {

private int code;
private String message;

public int getCode(){
    return code;
}

public String getMessage(){
    return message;
}

public ExceptionName(int code, String message) {
    this.code = code;
    this.message = message;
}

}

还要实现一个ExceptionMapper

@Provider
public class ExceptionName implements ExceptionMapper<ExceptionName>{

    @Override
    public Response toResponse(ExceptionName exception) {
        return Response.status(exception.getCode()).entity(exception.getMessage()).build();
    }

}

每次我想抛出异常时,我都会在任何地方这样做,异常映射器将负责向使用 API 的客户端返回响应

throw new ExceptionName(500,"there was an error with something here");
于 2017-07-13T18:08:22.267 回答
-1

一个小评论,尝试使用 Response.Status.NOT_FOUND 而不是使用 404 等。代码将更易读,更不容易出现拼写错误,“text/plain”也是如此。下面是处理您提到的异常的代码。 哦,还有一件事记得在你的界面中注释你的方法 @Produces(MediaType.TEXT_PLAIN)

    公共类 UserNotFoundException 扩展异常 {
        //...
    }

    公共类 UserServiceImpl 实现 UserService {

        @覆盖
        公共响应 getUser(@QueryParam("id") String id) {
            最终响应响应;
            尝试{
                // 调用用户方法
                //如果一切正常
                response = Response.status(Response.Status.OK).entity(whateverYouWant).type(MediaType.TEXT_PLAIN).build();
            } 捕捉(UserNotFoundException ex){         
                response = new UserNotFoundMapper().toResponse(ex);
            }

            返回响应;
        }
    }

    在客户幻灯片中,您可以检查

    public static boolean isUserExists(final Response serverResp) {
        return serverResp != null && serverResp.getStatus() == Response.Status.NOT_FOUND.getStatusCode();
    }

于 2013-03-03T12:25:51.210 回答