如果出现错误,泽西岛不会显示响应
我遇到以下问题...
我正在测试一项在 GET 请求上返回 HTTP 响应的服务。
我的问题是我想查看响应,即使它是 HTTP 500 / 404 或任何响应。
我想看看。但我不能,因为它会引发异常,仅此而已。
有没有办法查看球衣响应,即使它是错误响应?
我的代码是这样的:
webResource = client.resource(url);
response = webResource.queryParams(alertParams)
.header("x-token", token).get(String.class);
因此,当 get 收到来自服务的错误响应时,我将无法查看该响应,尽管响应是这样的:
{ “错误代码”:“ERR002”, "errMsg" : "", "techErrMsg" : "LoginFailureGeneric" 这
是一个 400 Bad Request。
非常感谢大家的帮助!!
I have the following problem...
I'm testing a service that return HTTP responses on GET requests.
My problem is that I would like to view the response even if it was an HTTP 500 / 404 or whatever response.
I would like to view that. But I can't because it throws an exception and that's it.
Is there a way to view a jersey response even if it was an error response?
My code is like this:
webResource = client.resource(url);
response = webResource.queryParams(alertParams)
.header("x-token", token).get(String.class);
So when get receives an error response from the service I wont be able to view that although the response is something like this:
{
"errCode" : "ERR002",
"errMsg" : "",
"techErrMsg" : "LoginFailureGeneric"
}
Which is a 400 Bad Request.
Thanks very much for all the help!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是您需要花一些时间阅读文档的地方...WebRequest#get(Class) 将引发异常href="http://jersey.java.net/nonav/apidocs/1.4/jersey/com/sun/jersey/api/client/ClientResponse.html">ClientResponse。
因此,您需要做的就是更改
.get(String.class)
->.get(ClientResponse.class)
并且您可以从 ClientResponse 对象中提取实体本身(以及状态和其他所有内容),不会出现异常。This is where you need to spend some time with the docs... WebRequest#get(Class) will throw an exception when you get an HTTP error status if you are trying to parse the response as anything other than ClientResponse.
So all you need to do is change the
.get(String.class)
->.get(ClientResponse.class)
and you can pull the entity itself (and the status, and everything else) off of the ClientResponse object sans exceptions.