wood burning stoves 2.0*
The moose likes EJB and other Java EE Technologies and the fly likes Using restful client which auto map objects into xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "Using restful client which auto map objects into xml" Watch "Using restful client which auto map objects into xml" New topic
Author

Using restful client which auto map objects into xml

ray frid
Ranch Hand

Joined: Dec 23, 2010
Posts: 79
I am using restEasy(Restful implementation for jboss) ejb3.0, Jboss5.1.1 AS

I did restful service which accepting simple object.

this is at the server side:



@POST
@Path("testObjects")
@Consumes("application/xml")
@Produces("text/plain")
public String testObjects(GrandSun sun)
{
System.out.println(sun.toString());
return "success";
}

this is the object which I have declared at the server side:

package com.mirs.wma.web.data;
import javax.xml.bind.annotation.XmlRootElement;


@XmlRootElement
public class GrandSun
{
int m = 1;
int g = 2;

}


I test it via restfull client which sending xml string and it works fine.

<?xml version="1.0" encoding="UTF-8"?>
<grandSun>
<m>111</m>
<g>22</g>
</grandSun>



What I am looking for is a restful client which will be able to send the whole object (as is) without needing me to convert manually to xml format.

Is there any option to do it via annotation?

I will just need to annotate the object at the client side and send it as is to the restful service?

thanks,
ntumba lobo
Ranch Hand

Joined: Oct 21, 2008
Posts: 180
From what I understand you want your data to travel as an object over the web between the client and the webservice.
If that's case we are not talking about web services anymore as for web services data must travel between client and service in a neutral format (XML).

But if I misunderstood and you just and to work with objects on the client side and use some kind of library to convert easily for you your objects to XML this is legitimate and
a good approach. For that I would suggest Spring3 MVC which has a good support for REST web services and the representations of your resources (XML, JSON ...) is handled by the framework
with very little configuration.

Hope that'll help

SCJP 5 , SCWCD 5, SCEA 5
ray frid
Ranch Hand

Joined: Dec 23, 2010
Posts: 79
Hi,

"But if I misunderstood and you just and to work with objects on the client side and use some kind of library to convert easily for you your objects to XML this is legitimate and
a good approach"

You were right about this one. thats what i want to do.

" I would suggest Spring3 MVC.."

We dont use that framework at our work. I am using here EJB's any other solution?

thanks,

ray.

ntumba lobo
Ranch Hand

Joined: Oct 21, 2008
Posts: 180
OK in that case you can just mark your objects with JAXB annotation and use the JAXB converter to convert obj<->XML.
There is very simple to put in place.

1. Annotate with JAXB your POJOs

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class MyClass {

@XmlElement(name="otherNameField")
private String myField;

....
}


2. Just create a utility class that uses the JAXB converter

for example your method convertObjectToXml() would be along the lines of :
MyClass myObject = new MyClass();
StringWriter stringWriter = new StringWriter();
Marshaller m = JAXBContext.newInstance(MyClass.class).createMarshaller();
m.marshal( myObject , stringWriter);

you probably also need convertXmlToObject() which is similar to the previous method.

I hope that'll help
ray frid
Ranch Hand

Joined: Dec 23, 2010
Posts: 79
Hi lobo,
I am getting your idea. but after I used

Marshaller m = JAXBContext.newInstance(MyClass.class).createMarshaller();
m.marshal( myObject , stringWriter);

which object is sending input from the kind 'Marshaller'

how do you post it on the stream? how send it over the HTTP to my rest service?

thanks,
ray.
ntumba lobo
Ranch Hand

Joined: Oct 21, 2008
Posts: 180
after using m.marshal( myObject , stringWriter); the result of the conversion is in the stringWritter object.

String xmlString = stringWriter.toString();

After that you use whatever HTTP client library you were using before to post your REST request.
I use quite often Apache HTTP Commons for example.
ray frid
Ranch Hand

Joined: Dec 23, 2010
Posts: 79
Hi,
I didnt understand that the result being sent to the stringWriter object. now I do

Thanks for your help body.

But I do wonder if I will have complex object structure instead of just fields(for example arraylist<String> . it will work the same? or should I annotate the object differently?
Thanks again,

ray.
ntumba lobo
Ranch Hand

Joined: Oct 21, 2008
Posts: 180
To handles collections or other oddities you should refer to the JAXB documentation (e.g JEE5 tutorial) to see the different ways of mapping various Java fields.
I am glad I could help you ;-)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using restful client which auto map objects into xml
 
Similar Threads
help needs to build this web service and do this exercise
Reading an object from ObjectInputStream
Data Access Object vs Value Object patterns
Regarding Usage of Web Services In desktop Swing application
Problem sending Apple Push Notification using Java and REST