Sending Object to Restful Web service Straight forward.

Hi,

We all know that Restful services works with HTTP protocol. restful services support xml/jamson.

Problem: I have object and I want to send it “as is” to a restful service.

Solutions: 1. not straight forward: We need to convert that object manually to XML string and send it.
2. Straight forward:
2.a Annotate the object with those annotations:
Class annotations:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
Fields annotations:
@XmlElement(name=”fieldName”)

example: (This has been tested on Jboss5.1.1 AS)

@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class SomeObject implements Serializable
{
	@XmlElement(name="m")
	int m = 1;
	@XmlElement(name="g")
	int g = 2;
}

Now in order to send it we need to marshell our object using StringWriter and Marshaller.
Example:

Client side:

                SomeObject  myObject= new SomeObject();
		HttpURLConnection conn = null;
		StringWriter stringWriter = new StringWriter();

		try
		{
			Marshaller m = JAXBContext.newInstance(SomeObject.class).createMarshaller();
			m.marshal(myObject, stringWriter);

			URL url = new URL("http://localhost:8080/MYPROJECT_WEB/hello
                        /testObjects");
			conn = (HttpURLConnection) url.openConnection();
			conn.setDoOutput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Type", "application/xml");


			OutputStream os = conn.getOutputStream();
			os.write(stringWriter.toString().getBytes());
			os.flush();

			BufferedReader br = new BufferedReader(new InputStreamReader((conn.getInputStream())));

			String output;
			System.out.println("Output from Server .... n");
			while ((output = br.readLine()) != null)
			{
				System.out.println(output);
			}

		}
		catch (Exception e)
		{

			e.printStackTrace();

		}
		finally
		{
			conn.disconnect();

		}

Server side:

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

Idan.

Related Articles:

(2078)

Creating Restful Services using EasyRest for Jboss

Hi,
Here is how we creating simple GET method using EasyRest(Supported perfectly by Jboss).
The method expecting one input of type String and print out it’s value.

First of all if you using Enterprise Application project make sure you add the Rest methods to the Web project.

The class which holds the method logic named: “RestFacade”

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

@Path("hello")
public class RestFacade
{
	@GET
	@Path("sayHello")
	public void sayHello(@QueryParam("input") String input)
	{
		System.out.println(input);
	}
}

* With QueryParam we declaring our rest method to expect input parameter(in our example it’s String).
* with GET we declaring our method to work when GET being invoked.

Now we need to add the following code to WEB.XML:

< ?xml version="1.0" encoding="UTF-8"?>

	YOUR_PROJECT_NAME
	
		index.html

	
	
		resteasy.scan
		true
	

	
		org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap


	
		Resteasy
		org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher


	
		Resteasy
		/*



* make sure you add the right project name and don’t forget to add the listener as shown below.

This is the url in order to trigger the method:

http://localhost:8080/MY_PROJECTWeb/hello/sayHello?input=TestingMyMethod

and the output is: 14:24:01,296 INFO [STDOUT] TestingMyMethod

Idan.

Related Articles:

(2078)