How to obtain the error description text for processing errors that do not return 200 in apache cxf

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.