• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using restful client which auto map objects into xml

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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,
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
ray frid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ;-)
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic