JAX-RS : A Way to Serialize String to XML as a Response?
Tim Clotworthy
Greenhorn
Joined: Feb 07, 2011
Posts: 7
posted
0
Hello,
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:
is there a way to return it from the subresource as a valid XML response? If this is not possible to do in JAX-RS, then how about a way within a conventional servlet? Thanks for any response!
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35223
7
posted
0
What do you mean by "valid XML response"? What prevents from returning precisely that XML in the response?
Note that "valid" in an XML context implies there being a schema involved, whether it be a DTD, XSD or RelaxNG; is that the case here?
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!
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35223
7
posted
0
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.
William Brogden
Author and all-around good cowpoke
Rancher
Joined: Mar 22, 2000
Posts: 12265
1
posted
0
If I return it as a java String that "looks like" XML, an XML parser will not be able to parse it as XML.
Why not?
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.
@XmlType(name="Book", propOrder={"id","titre","maisonEdit","auteur"})
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Book")
@Path("Book")
public class Book implements Serializable{
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
@Path("library")
public class Library {
@GET
@Produces({"application/xml", "application/json"})
public String getBook(){
Book book = new Book("La vie","Larousse","Harry",1234);
return book.toString();
}
}
this one is my web.xml
///////////////////////////////////////////////////////web.xml/////////////////////////////////
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>Application</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>Biblio</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
now I don't have any more classes or xml datei
but when I deploy my projet it's work very well . The only one Problem is that I receive
on my browser this message
XML-Verarbeitungsfehler: nicht wohlgeformt
Adresse: http://127.0.0.1:8080/Library-war/library/library Zeile Nr. 1, Spalte 12:Biblio.Book@7
please help me I don?t know How I can do to have a xml answer on my browser
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35223
7
posted
0
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.