在负面的 REST 响应中缺少主体

huangapple go评论97阅读模式
英文:

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 &quot;http://10.0.0.4:49152/rest/geo?loc=12345&amp;get=description&quot; (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(&quot;text/plain&quot;)
@Override
public String get(@Context UriInfo uriInfo, @Context Request request)
{
	String queryString = uriInfo.getRequestUri().getQuery();
    //etc
    if (somethingIsWrong)
    {
        // I don&#39;t see this message in the response
        throw new BadRequestException(&quot;GET request issue - something is wrong&quot;);
    }
    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)
&gt; GET /rest/geo?loc=12345&amp;get=description HTTP/1.1
&gt; Host: 10.0.0.4:49152
&gt; User-Agent: curl/7.55.1
&gt; Accept: */*
&gt;
&lt; HTTP/1.1 400 Bad Request
&lt; Date: Thu, 22 Oct 2020 14:20:37 GMT
&lt; Content-length: 0
&lt;
* 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&lt;Throwable&gt; {

    @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 = &quot;Server error&quot;;
        }

        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.

huangapple
  • 本文由 发表于 2020年10月22日 22:49:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/64484824.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定