如何在BadRequestException中包含一条消息



是否可以在BadRequestException中包含一条消息,因此当用户看到响应代码A 400时,他/她还可以弄清楚为什么?

场景将是这样简化的:

public Entity getEntityWithOptions(@PathParam("id") String id, @QueryParam("option") String optValue) {
    if (optValue != null) {
        // Option is an enum
        try {
            Option option = Option.valueOf(optValue);
        } catch (IllegalArgumentException e) {
            throw new BadRequestException(e.getMessage());
        }
        return new Entity(option);
    }
    return new Entity();
}

我知道这可以返回 Response对象,但我不想要那个。

这可能吗?也许有ExceptionMapper<BadRequestException>?否则无法完成此操作,因为BadRequestException已经是泽西特定的例外了?

有一种非常简单的方法,如下所示。

Response.ResponseBuilder resp_builder=Response.status(Response.Status.BAD_REQUEST);
resp_builder.entity(e.getMessage());//message you need as the body
throw new WebApplicationException(resp_builder.build());

如果您需要添加标头,响应媒体类型或其他功能,则响应Builder 提供了所有功能。

您可以将Customexception扔给CustomeXceptionMapper以提供自定义的响应。

public class CustomException extends RuntimeException {    
    public CustomException(Throwable throwable) {
        super(throwable);
    }
    public CustomException(String string, Throwable throwable) {
        super(string, throwable);
    }
    public CustomException(String string) {
        super(string);
    }
    public CustomException() {
        super();
    }
}
@Provider
public class CustomExceptionMapper implements ExceptionMapper<CustomException> {
    private static Logger logger = Logger.getLogger(CustomExceptionMapper.class.getName());
    /**
     * This constructor is invoked when exception is thrown, after
     * resource-method has been invoked. Using @provider.
     */
    public CustomExceptionMapper() {
        super();
    }
    /**
     * When exception is thrown by the jersey container.This method is invoked  
     */
    public Response toResponse(CustomException ex) {
        logger.log(Level.SEVERE, ex.getMessage(), ex);
        Response.ResponseBuilder resp = Response.status(Response.Status.BAD_REQUEST)
                        .entity(ex.getMessage());
        return resp.build();
    }
}

使用像这样的代码中的Customexception。

public Entity getEntityWithOptions(@PathParam("id") String id, 
                                   @QueryParam("option") String optValue) 
                                   throws CustomException {
    if (optValue != null) {
        // Option is an enum
        try {
            Option option = Option.valueOf(optValue);
        } catch (IllegalArgumentException e) {
            throw new CustomException(e.getMessage(),e);
        }
        return new Entity(option);
    }
    return new Entity();
}

而不是消息,您还可以通过Customexception构建对象并将其传递给映射器。

您应该创建一个自定义异常

public class CustomBadReq extends WebApplicationException {
     public CustomBadReq(String message) {
         super(Response.status(Response.Status.BAD_REQUEST)
             .entity(message).type(MediaType.TEXT_PLAIN).build());
     }
}

另请参见此

您可以使用BadRequestException(Response response)构造函数进行操作。

例如:

String msg = e.getMessage();
throw new BadRequestException(Response.status(BAD_REQUEST)
                .entity(msg).build());

最新更新