tags:

views:

513

answers:

2

Hi.

I am writing a RESTful web service where in I want to return a XML containing some resultset. I have used XSTREAM and parsed the object into XML string. Since I need to return this string, I need to know how to pass it back to the calling client.

One way is to return the RESPONSE to the calling client. And my sample code here shows what it is that I am trying to do.

@Path("somepath")
public class ClassToReturnXML
{
    public Response methodToReturnXML()
    {

       ResponseBuilder builder = new ResponseBuilderImpl();
       builder.type(MediaType.TEXT_XML);
       builder.entity(myXMLString);
       return builder.build();
    }
}

Unfortunately it doesn't return the entity, though the status code is 200. Am I instantiating the ResponseBuilder incorrectly? I also saw somewhere that it should be instantiated as follows:

ResponseBuilder builder = Response.status(200);

Please suggest what is the apt way to return XML in response.

I AM USING APACHE CXF for RESTFUL SERVICES. (Version 2.2.3 -- i guess) :D Thanks in Advance for all the help.

A: 

Does the HTTP Response have the correct content-type header to identify that it is Xml i.e. text/xml or application\xml? Checkout The Proper Content Type for XML Feeds.

The response status 200 is just one of the standard HTTP Response Codes that means the request has succeeded so only return it if that is the case.

Dave Anderson
+1  A: 

It was just a cleaning problem. It eventually worked. I created the response in following way eventually.

Response response = Response.status(200).type(MediaType.TEXT_XML).entity(xmlString).build();

It works just fine. I hope it helps someone.

Priyank