Java – Jersey doesn’t show a response in case it goes wrong

I have the following questions

@H_ 502_ 8@

I'm testing a service that returns an HTTP response to a get request@ H_ 502_ 8@

My problem is that I want to view the response, even if it is HTTP 500 / 404 or any response@ H_ 502_ 8@

I would like to see it. But I can't, because it throws an exception, that's it@ H_ 502_ 8@

Is there a way to view the Jersey response, even if it is an error response@ H_ 502_ 8@

My code is like this: @ h_ 502_ 8@

@H_ 502_ 8@

webResource = client.resource(url);

response = webResource.queryParams(alertParams)
        .header("x-token",token).get(String.class);

Therefore, when get receives an error response from the service, I will not be able to view it, even though the response is like this: @ h_ 502_ 8@

{ “errCode”:“ERR002”, “errMsg”:“”, “techErrMsg”:“LoginFailureGeneric” }@H_ 502_ 8@

This is a 400 bad request@ H_ 502_ 8@

Thank you very much for all your help@ H_ 502_ 8@

Solution

This is where you need to spend some time using the document... If you try to parse the response into anything other than clientresponse, webrequest#get (class) will throw an exception when you get the HTTP error status

@H_ 502_ 8@

So what you need to do is change get(String.class) – > . Get (clientresponse. Class), you can pull the entity itself (as well as the state and all other contents) from the clientresponse object, but excluding exceptions@ H_ 502_ 8@

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>