英文:
Missing body in negative REST responses
问题
以下是翻译好的内容:
我已经创建了自己的REST类,并且正在使用curl -v "http://10.0.0.4:49152/rest/geo?loc=12345&get=description"
(例如)进行调用。我还可以使用httpClient.send
从Java中调用它们。
在这两种情况下,如果REST类返回一个字符串,那么一切都正常 - 消息主体包含该字符串;但是如果我的REST类抛出异常,返回的对象/消息就不包含我的自定义消息:
@GET
@Produces("text/plain")
@Override
public String get(@Context UriInfo uriInfo, @Context Request request)
{
String queryString = uriInfo.getRequestUri().getQuery();
//等等
if (somethingIsWrong)
{
// 我在响应中看不到这条消息
throw new BadRequestException("GET请求问题 - 出现错误");
}
else
{
return correctString;
}
}
响应是状态码400,这是我预期的,但这是我的输出:
* 正在尝试 10.0.0.4...
* 已设置 TCP_NODELAY 特性
* 已连接到 10.0.0.4(10.0.0.4),端口 49152(#0)
> GET /rest/geo?loc=12345&get=description HTTP/1.1
> 主机: 10.0.0.4:49152
> 用户代理: curl/7.55.1
> 接受: */*
>
< HTTP/1.1 400 Bad Request
< 日期: 周四, 22 十月 2020 14:20:37 GMT
< 内容长度: 0
<
* 与主机 10.0.0.4 的连接保持不变
同样,在Java的HttpResponse消息中,我可以看到代码(400),但看不到异常中的消息。
如何查看返回的消息?
英文:
I have created my own REST classes, and am calling the using curl -v "http://10.0.0.4:49152/rest/geo?loc=12345&get=description"
(for example). I also call them from java using httpClient.send
.
In both cases, if the REST class returns a string then everything is fine - the message body contains the String; but if my REST class instead throws an exception, the returned object/message doesn't contain my custom message:
@GET
@Produces("text/plain")
@Override
public String get(@Context UriInfo uriInfo, @Context Request request)
{
String queryString = uriInfo.getRequestUri().getQuery();
//etc
if (somethingIsWrong)
{
// I don't see this message in the response
throw new BadRequestException("GET request issue - something is wrong");
}
else
{
return correctString;
}
}
The response is a code 400, which is what I'd expect, but this is my output:
* Trying 10.0.0.4...
* TCP_NODELAY set
* Connected to 10.0.0.4 (10.0.0.4) port 49152 (#0)
> GET /rest/geo?loc=12345&get=description HTTP/1.1
> Host: 10.0.0.4:49152
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 400 Bad Request
< Date: Thu, 22 Oct 2020 14:20:37 GMT
< Content-length: 0
<
* Connection #0 to host 10.0.0.4 left intact
Likewise in the java HttpResponse message I can see the code (400) but not the message in my Exception.
How can I see the returned message?
答案1
得分: 1
这可能是一个与容器相关的问题。为了彻底解决这类问题,我个人使用ExceptionMapper
来处理,就像这样:
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class RestExceptionMapper implements ExceptionMapper<Throwable> {
@Override
public Response toResponse(Throwable t) {
Object entity;
Response.Status status;
if (t instanceof SomeException) {
status = // 计算状态
entity = t.getMessage();
} else {
status = Response.Status.INTERNAL_SERVER_ERROR;
entity = "服务器错误";
}
return Response
.status(status)
.type(MediaType.TEXT_PLAIN)
.entity(entity)
.build();
}
}
通过@Provider
注解进行标注,该类将会被容器自动发现。
英文:
This is probably a container-specific issue. To solve such problems once and for all, I personally make use of ExceptionMapper
like this:
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
@Provider
public class RestExceptionMapper implements ExceptionMapper<Throwable> {
@Override
public Response toResponse(Throwable t) {
Object entity;
Response.Status status;
if (t instanceof SomeException) {
status = // compute status
entity = t.getMessage();
} else {
status = Response.Status.INTERNAL_SERVER_ERROR;
entity = "Server error";
}
return Response
.status(status)
.type(MediaType.TEXT_PLAIN)
.entity(entity)
.build();
}
}
Annotated with the @Provider
annotation, the class will be automatically discovered by the container.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论