Symptom:
We encountered some problems when using the apache cxf framework. Generally, if we use the rest client, if the returned results are correct, we can easily handle them, however, if the returned request return code is not 200, we can find that the exception object does not contain any error message text during debugging:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11263Q011-0.png "title =" 31.png"/>
For example, we use restclient for testing this request. The return code is 400. The returned response contains the error description field. We want to obtain the content of this field, but we tried it many times, it cannot be obtained directly from the exception object, that is, it can be obtained directly without a similar field or API method.
Our attempt (debug) is as follows:
650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/11263Q493-1.png "title =" 32.png"/>
It can be seen from this that there is no field here to directly obtain the content corresponding to our description section, so our attempt failed.
New breakthroughs:
A good engineer has come up with a good solution. In fact, this content is not nothing, but not directly. In fact, this content has already been returned, its content is put in the form of byte streams in response. in the entity field, we can see from the above, this response. the content corresponding to entity is an instance of an internal class of HttpURLConnection $ HttpInputStream, so we must open this byte stream and then construct the content we need.
The solution is as follows:
In our catch () exception handling Branch:
Catch (WebApplicationException e) {LOGGER. error ("webapplicationexception:" + e. getResponse (). getStatus (); // This row gets status codeInputStream in = (InputStream) e. getResponse (). getEntity (); // The input stream contains all the text information about the error message byte [] buffer; try {buffer = new byte [in. available ()]; in length; // open and read the input stream, and finally combine the content into a string while (length = in. read (buffer, 0, buffer. length ))! =-1) {output = new String (buffer, "UTF-8") ;}} catch (IOException ex) {LOGGER. error (ex. getMessage ());}}...
This article from "parallel line cohesion" blog, please be sure to keep this source http://supercharles888.blog.51cto.com/609344/1259805