views:

126

answers:

3

Hi,

This may sound like a simple question but as am newbie in Webservies and this is my first time using it and so am asking my doubt.

Q: How can I pass objects or complex types using Web Services ? I have created a simple webservice and am passing string and integer types but I am not sure how can I pass objects using webservice and so any guidance would be highly appreciated.

Thanks.

+1  A: 

You only have to serialize the object (make a text) on the service-side and de-serialze (make an object again) on the receiver-side. For years SOAP was standard for this, but today JSON becomes more popular as it has lot less overhead than SOAP.

If using SOAP and Java you may try GSON by Google, which provides a very easy-to-use programming interface.

JSON with GSON:

String jsonized = new Gson().toJson( myComplexObject ); 
/* no we have a serialized version of myComplexObject */ 

myComplexObjectClass myComplexObjext = new Gson().fromJson( jsonized, myComplexObjectClass.class ); 
/* now we have the object again */

For JSON with JAX-WS (we don't use Apache Axis) have a look at these starter-tutorials:

heb
Can you share an two example of doing this, one by SOAP and other by JSON ?
Rachel
In which programming language?
heb
Java is the language which we are using.
Rachel
SOAP is much more complex with Java as you need skeleton classes for each class you want to serialize. You can use Apache AXIS for this, but it is not easy to handle! If JSON does cover your needs, use JSON.
heb
Can you please update this in answer ? It is not readable in comments
Rachel
Thank you heb for formatting it, I am able to get your point in here, also I am using Axis2 as SOAP Engine for my implementation, what would be best approach in that scenario to send complex types like Objects ?
Rachel
Also lets say if am using JSON than can I use it with Axis2, I know this question does not make sense but as am new to it, I thought of getting your feedback on this.
Rachel
Indeed this doesn't makes sense ;) Axis2 is a full-featured solution for SOAP-Web-Services, JSON/GSON is only a library for creating and parsing JSON-objects, so you will have to add your own web-service (by using Tomcat or Jetty or similar) to provide these serialized JSON-objects to the clients. Of course you could create a JSON-string and wrap it into a SOAP-webservice - but this would undermine the idea of JSON as a lightweight representation of serialized objects.
heb
I thought so...but just wanted to get you input on it. I was thinking of getting objects jsonized and then dejsonized at the client using Axis but then it will not make more sense as I am still using `SOAP`, do we have anything similar to `Axis2/CFX` which are `SOAP Engine` for `JSON` which could act as `JSON Engine`, if this does not make sense then its ok but share your thoughts on it also `;)`
Rachel
@heb: Also `http://myarch.com/create-jax-ws-service-in-5-minutes` does not work, can you update it.
Rachel
myarch... works fine for me. As for a JSON "engine" you can serve JSON with a HttpServlet-container (like Tomcat, Jetty) and receive them with any URL-object from within Java (or any other language). Don't mixup JSON with SOAP - they are two completely different ways for object serialization.
heb
A: 

You could pass json or use xmlserialization if needed.

Byron Cobb
Can you elaborate more on your point give some detail explanation and example to support the argument so that I have some better understanding of the concept ?
Rachel
+1  A: 

If you are using restful web services (I'd recommend Jersey if you are http://jersey.dev.java.net) you can pass JAXB annotated objects. Jersey will automatically serialize and deserialize your objects on both the client and server side.

Server side;

@Path("/mypath")
public class MyResource
{
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public MyBean getBean()
    {
        MyBean bean = new MyBean();
        bean.setName("Hello");
        bean.setMessage("World");
        return bean;
    }

    @POST
    @Consumers(MediaType.APPLICATION_XML)
    public void updateBean(MyBean bean)
    {
        //Do something with your bean here
    }
}

Client side;

//Get data from the server
Client client = Client.create();
WebResource resource = client.resource(url);
MyBean bean = resource.get(MyBean.class);

//Post data to the server
bean.setName("Greetings");
bean.setMessage("Universe");
resource.type(MediaType.APPLICATION_XML).post(bean);

JAXB bean;

@XmlRootElement
public class MyBean
{
    private String name;
    private String message;

    //Constructors and getters/setters here
}
Qwerky
@qwerky = I am not using `REST` but `SOAP`, can you share an example with `SOAP` ?
Rachel