SOAP to XML conversion and sending it in response object - java

I am new to webservices,some how i managed to host a service with metro.With the help of SOAP UI and also httpUrlConnection object i am able to get SOAP response.But my next task is to send a response with content type "application/xml".So i used httpServletResponse,but i am not getting how to extract only XML part(without SOAP envolope and SOAP header) and also how to send XML inside respose object.Whether the way in which i am doing is rite?If yes,how to proceed with next step.

There are various ways to send xml content within SOAP response. To keep wsdl and xsd simple, you may use CDATA for your xml content and parse like a string on the receiving end.
<![CDATA[YOUR XML GOES HERE]]>
If you want to parse SOAP message and extract XML then you may want to use Java API SAAJ

Related

How to take xml input from postman request body

I am creating a java server with Core java and want to create an Document object. Currently I am using InputStreamReader. I am already entring xml into the request body, Please tell me how to store the request body in The Document obj using core java, No frameworks

Sending XML in Query String

I am integrating USPS Web API in my Java application. I have to send a request of the form
http://production.shippingapis.com/ShippingAPI.dll?API=CityStateLookup&XML=<CityStateLookupRequest%20USERID="XXXNORTH3110"> <ZipCode ID= "0"> <Zip5>22102</Zip5> </ZipCode> </CityStateLookupRequest>
Now hitting this on the browser works fine. But using this from the JAVA code breaks. How can I send XML in the Query String?
Why you should use POST to send this type of data: http://www.w3schools.com/tags/ref_httpmethods.asp
In the POST body you don't need to encode the XML, you just need to set the correct content type "application/xml". Of course that only applies if it is valid XML and does not contain char are not allowed by XML standard.

How can I extract an XML payload from SOAP message with Java?

I am new to SOAP and need to extract an XML payload from a SOAP message. Our application is contacting a soap service, and the service is returning an XML Document that is embedded within a SOAP message. I need to extract this XML that is within the SOAP body, and save it as a "stand alone" well formed XML document. What is the best way to go about this?

Handling different XML response documents with one SAX Handler

I am developing a Java application that makes an HTTP Request to a web service, and XML is returned. If the response code is 200, then a requestSucceeded() callback method will send the XML to a SAXParser with a different SAX Handler, depending on what web service is being called. If the response code is not 200, then a requestFailed() callback method is being called.
The web service that I am calling will return two types of XML documents (with a response code of 200): an XML document containing the successful response information, or an XML error document containing error information (for example, if one of the request parameters wasn't formatted correctly).
My question is this: Given my current setup, what is the best way to look for / handle both kinds of XML documents (a successful XML response or an XML error document)? The SAX Handler is looking for all of the relevant response information and it is storing that information into an object, which is then processed by my application. Is there a better solution than just always first looking for the unique XML Error tags?
Thanks!
Option #1 - Change Respose Code
Why are you returning an error with response code 200? 400 (Bad Request) or another error code might be a better option. Then you could process the XML based on the response code.
Option #2 - Swap Content Handlers
Below is a link to one of my previous answers where I explain how to swap content handlers while processing the document. You could have one content handler that determines if the response is content or error, and then swaps in the appropriate content handler to process the rest.
Using SAX to parse common XML elements
Option #3 - Use JAXB
If the end result is that the XML will be converted to an object, have you considered using JAXB? It will build an object based on the XML based on what is returned.

Send error message as JSON object

I have two servlet: first servlet is similar to a client and creates an HttpURLConnection to call the second servlet.
I would like send a special error, formatted like a JSON object, so I call sendError method in this way:
response.sendError(code, "{json-object}")
But in the first servlet when I read error with getResponseMessage method I just get standard HTTP message and not my json object as a string.
How I can get my json string?
From the HttpServletResponse#sendError() javadoc:
The server defaults to creating the response to look like an HTML-formatted server error page containing the specified message, setting the content type to "text/html", leaving cookies and other headers unmodified. If an error-page declaration has been made for the web application corresponding to the status code passed in, it will be served back in preference to the suggested msg parameter.
So with this approach you have no other option than extracting the message from the HTML response yourself. JSoup may however be useful in this.
To achieve what you want, you need to set the error code and write the response yourself, e.g.
response.setStatus(code);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
Instead of code you could by the way also use one of the HttpServletResponse.SC_XXX constants for this.

Categories