如何在BadRequestException中包含消息?

时间:2016-06-29 15:46:30

标签: java web-services http jersey jax-rs

是否可以在o中添加消息,因此当用户看到响应代码为400时,他/她还可以找出原因?

场景将是这样的,简化:

BadRequestException

我知道可以通过返回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>已经是特定于泽西岛的例外?

4 个答案:

答案 0 :(得分:1)

您可以抛出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将其传递给mapper,而不是消息。

答案 1 :(得分:1)

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

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

如果您需要添加标题,响应媒体类型或其他一些功能, ResponseBuilder 会提供所有功能。

答案 2 :(得分:0)

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

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

另见this

答案 3 :(得分:0)

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

例如:

<root>
<child>
<RequestHeader>ABC</RequestHeader>
<RequestBody>DEF</RequestBody>
<ResponseHeader>GHI</ResponseHeader>
<ResponseBody>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>cccc</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="./js/util.js"></script>
</head><body>

<center>
<table width="80%" class="border">
<tr BGCOLOR=#C3D9FF>
<td align="center" colspan="6">
<H1>ABC</H1>
<table width="100%" class=\"noborder\">
<tr BGCOLOR=#C3D9FF>
<td align="center" width="30%">&nbsp;</td>
<td align="center" width="40%">Wxxxx</td>
<td align="center" width="30%" style="text-align: right" >
Guest user</html>
</ResponseBody>
</child>
<child>
<RequestHeader></RequestHeader>
<RequestBody></RequestBody>
<ResponseHeader></ResponseHeader>
<ResponseBody>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<html>
<head>
<title>aaaa</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="./js/util.js"></script>
</head><body>

<center>
<table width="80%" class="border">
<tr BGCOLOR=#C3D9FF>
<td align="center" colspan="6">
<H1>XYZ</H1>
<table width="100%" class=\"noborder\">
<tr BGCOLOR=#C3D9FF>
<td align="center" width="30%">&nbsp;</td>
<td align="center" width="40%">xxxx</td>
<td align="center" width="30%" style="text-align: right" >
Guest user</html>
</ResponseBody>
</child>
</root>