How to do REST invocation with Axis2/Java ServiceClient?

Axis2 ServiceClient enables one to invoke a service in a RESTfull manner, and that can be used to invoke a REST service as well. Let's see how this can be done.
Date: Sat, 17th Jun, 2006
Level:
Reads: 14328 Comments: 1 | Login or register to post comments
Eran Chinthaka
Software Engineer
WSO2 Inc.
chinthaka's picture
The difference between doing a SOAP invocation and a REST invocation depends on just one line (with default configuration). Just insert the following line of code:
 options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);
Let's see the complete code snippet.
Options options = new Options();
options.setTo(new EndpointReference(toEpr));

options.setProperty(Constants.Configuration.ENABLE_REST, Constants.VALUE_TRUE);

ServiceClient sender = new ServiceClient();
sender.setOptions(options);
OMElement result = sender.sendReceive(getPayload());
If you are invoking a Web service in RESTful manner, make sure the toEpr you give has all the information required to identify the operation and the service that you are invoking. The above code will invoke a resource using HTTP PUT method. To change it to use GET method simply set another option as shown below.
 
options.setProperty(Constants.Configuration.HTTP_METHOD,
Constants.Configuration.HTTP_METHOD_GET);
Can I change the content type of the request? Yes, you can.
 
options.setProperty(Constants.Configuration.CONTENT_TYPE, contentType);
The content type should be one of the following:
  1. MEDIA_TYPE_X_WWW_FORM - application/x-www-form-urlencoded
  2. MEDIA_TYPE_TEXT_XML - text/xml
  3. MEDIA_TYPE_MULTIPART_RELATED - multipart/related
  4. MEDIA_TYPE_APPLICATION_XML - application/xml
kamaldeep.tiainterweb.com's picture

Very helpful post.

Very helpful post.
library project main code
Learn Cloud
Learn
Cloud

The WSO2 Application Server is a reliable application server that can host your enterprise web applications. The WSO2 Application Server as a Service is offered in StratosLive, the WSO2 Platform as a Service. This article explains how a simple web application can be developed and deployed from Carbon Studio to the WSO2 Application Server...

Latest Webinar
Different groups within an organization need to monitor different Key Performance Indicators (KPIs) - An operations team will be interested in the response times of business services and loads of each service,..
Thursday, February 9th 2012, 09.00 AM (PST)

Thursday, February 9th 2012, 10.00 AM (GMT)