aspose file tools*
The moose likes Web Services and the fly likes JAX-RS : A Way to Serialize String to XML as a Response? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "JAX-RS : A Way to Serialize String to XML as a Response?" Watch "JAX-RS : A Way to Serialize String to XML as a Response?" New topic
Author

JAX-RS : A Way to Serialize String to XML as a Response?

Tim Clotworthy
Greenhorn

Joined: Feb 07, 2011
Posts: 7
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:

<ElemA>
<ElemB>
<ElemC>Value</ElemC>
</ElemB>
</ElemA>

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: 42292
    
  64
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?


Ping & DNS - my free Android networking tools app
Tim Clotworthy
Greenhorn

Joined: Feb 07, 2011
Posts: 7
Thank you for response.

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: 42292
    
  64
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: 12806
    
    5
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.

Bill
gasparri Mbeunzoueuh
Greenhorn

Joined: Feb 25, 2011
Posts: 3
Hi, I thimk i have the same problem.
I have a simple class Book and Library
this is a copy of book



///////////////////////////////////////////////////////////////////Book/////////////////////////////////////////////
package Biblio;

import java.io.Serializable;

import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;


@XmlType(name="Book", propOrder={"id","titre","maisonEdit","auteur"})
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name="Book")
@Path("Book")
public class Book implements Serializable{


@XmlElement(name = "id")
private int id_book;
@XmlElement(name = "titre")
private String titre;
@XmlElement(name = "maisonEdit")
private String maisonEdit;
@XmlElement(name = "auteur")
private String auteur;


@Produces({"application/xml", "application/json"})
@Override

public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Book other = (Book) obj;
if (this.id_book != other.id_book) {
return false;
}
return true;
}

public Book(){}

public Book(String pTitre, String pMaisonEdit, String pAuteur, int pId_book) {
this.titre = pTitre;
this.maisonEdit = pMaisonEdit;
this.auteur = pAuteur;
this.id_book = pId_book;
}

public Book(String pTitre, String pMaisonEdit, int pId_book) {
this.titre = pTitre;
this.maisonEdit = pMaisonEdit;
this.id_book = pId_book;
}


@Override
public int hashCode() {
int hash = 7;
return hash;
}


public int getId_book() {
return id_book;
}

public void setId_book(final int pId_book) {
this.id_book = pId_book;
}

public String getAuteur() {
return auteur;
}

public void setAuteur(String pAuteur) {
this.auteur = pAuteur;
}

public String getMaisonEdit() {
return maisonEdit;
}

public void setMaisonEdit(String pMaisonEdit) {
this.maisonEdit = pMaisonEdit;
}

public String getTitre() {
return titre;
}

public void setTitre(String pTitre) {
this.titre = pTitre;
}


}
and this Library


/////////////////////////////////////////////Library//////////////////////////////////////////
package Biblio;

import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;

/*
* 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>

</servlet>
<servlet-mapping>
<servlet-name>Application</servlet-name>
<url-pattern>/library/*</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>




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: 42292
    
  64
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.
gasparri Mbeunzoueuh
Greenhorn

Joined: Feb 25, 2011
Posts: 3
Thanks to all it is working now
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: JAX-RS : A Way to Serialize String to XML as a Response?