How to pass complex types like objects using Webservices?

This may sound like a simple question, but as a beginner at Webservies, and this is my first time using it, and so I ask my doubts.

Q: How to pass objects or complex types using web services? I created a simple web service and pass string and integer types, but I'm not sure how to pass objects using webservice, and so any guidance would be highly appreciated.

Thanks.

+6
java soap serialization web-services soa
source share
3 answers

You only need to serialize the object (make the text) on the service side and de-serialize (make the object again) on the receiver side. For many years, SOAP has been standard for this, but today JSON is becoming more popular as it has much less overhead than SOAP.

If you use SOAP and Java, you can try Google's GSON, 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 do not use Axis Axis) take a look at these starting tutorials:

+7
source share

If you use calm web services (I would recommend Jersey if you are http://jersey.dev.java.net ), you can pass annotated JAXB objects. Jersey will automatically serialize and deserialize your objects both on the client side and on the 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 } 
+3
source share

You can pass json or use xmlserialization if necessary.

0
source share

All Articles