JAX-RS : A Way to Serialize String to XML as a Response?
Joined: Feb 07, 2011
Ok, I have a situation where I do not know ahead of time (i.e., runtime) the exact structure of a subresource's return value. However, I DO know that it is supposed to be valid XML. If this XML is presented to me as a String, is there is a way to take this String representation of this XML structure and have it returned as an XML encoded response?
In other words, if I have a java String that looks like:
We should forget the word "valid". just say "XML". I will receive a java String that "looks like" xml, and I have to output it as XML. However, we all know that a java String that "looks like" XML is not actually XML. It has to be encoded as XML in order for the response to be streamed back to the client as XML. If I return it as a java String that "looks like" XML, an XML parser will not be able to parse it as XML.
Is that any clearer? Is there a way to transform this java String that "looks like" XML to XML encoded output before I send it in the response from my JAX-RS based web service?
In this current JAX-RS web service, this java String currently is actually wrappered in java objects that get serialized (thanks to JAX-RS/JAXB) to XML. However, once this XML-wrappered-java-String gets processed by the XML parser on the client, the parser successfully parses the XML wrapper part, and then recognizes the String part for what it is; a java String.
I hope this is clearer. I would be grateful for any and all ideas for how to solve my problem!
Joined: Mar 22, 2005
I don't follow where you see a problem with streaming XML from a JAX-RS web service; maybe that's because I don't understand the difference between "encoded XML" and "unencoded XML". What prevents the web service from returning XML, or what would prevent a client from parsing it if the service did return XML?
The parser would, of course, need to take into account the encoding of the HTTP response, as laid out in the Content-Type header, and that should jell with the encoding of the XML content as specified in the <xml> element.
Author and all-around good cowpoke
Joined: Mar 22, 2000
If I return it as a java String that "looks like" XML, an XML parser will not be able to parse it as XML.
XML is just text in a defined format. If the String is in that format, it can be turned into an input stream that will satisfy the XML parser.
please help me I don?t know How I can do to have a xml answer on my browser
Joined: Mar 22, 2005
Book.toString() returns a JVM-internal representation of the Book object, not XML or JSON. The value returned is actually listed in the error message, it's "Biblio.Book@7" which is, of course, not well-formed XML.
The getBook method should "return book", and you need to configure a JAXBContext resolver. Jersey comes with examples that show how to do that.
In the future, please UseCodeTags when posting code of any length. It's unnecessarily hard to read the code as it is, making it less likely that people will bother to do so.