SOA / Web Services / Java

A Technology Blog

REST Client using Apache Wink

Posted by Vivek on May 7, 2016

This post shows how to write a client in java using apache wink framework to invoke a REST based service. Apache Wink is a powerful framework to build RESTful webservices and has both client and server modules. We will use the client module which is built on top of the JDK HttpURLConnection.

  1. Getting the dependencies: You need to get all the jars or libraries in place. You can use maven to download all the jars. These are jars that we will keep handy. Please note that not all jars are required but for different REST service and testing requirements, you will need them sooner or later.

activation-1.1.jar
analytics-core-5.2.1.jar
animal-sniffer-annotations-1.11.jar
bcprov-jdk15-144.jar
binding-2.0.1.jar
cajo-1.142.jar
commons-beanutils-1.7.0.jar
commons-cli-1.2.jar
commons-codec-1.3.jar
commons-collections-3.2.1.jar
commons-httpclient-3.1.jar
commons-io-2.4.jar
commons-lang-2.4.jar
commons-logging-1.1.1.jar
cssparser-0.9.5.jar
dom4j-1.6.1.jar
ezmorph-1.0.5.jar
flex-messaging-common-1.0.jar
flex-messaging-core-1.0.jar
flex-messaging-opt-1.0.jar
flex-messaging-proxy-1.0.jar
flex-messaging-remoting-1.0.jar
forms-1.0.7.jar
groovy-all-2.1.7.jar
guava-14.0.jar
hamcrest-core-1.3.jar
hermes-1.14.jar
htmlunit-2.7.jar
htmlunit-core-js-2.7.jar
httpclient-4.1.1.jar
httpclient-cache-4.1.1.jar
httpcore-4.1.1.jar
httpcore-nio-4.1.1.jar
httpmime-4.1.1.jar
i4jruntime-5.1.14.jar
javassist-3.16.1-GA.jar
jaxen-1.1-beta-8.jar
jcifs-1.2.9.jar
jdom-1.0.jar
jettison-1.2.jar
jetty-6.1.26.jar
jetty-util-6.1.26.jar
jms-1.1.jar
joda-time-1.6.2.jar
js-1.7R2.jar
json-lib-2.2.2-jdk15.jar
json-path-0.9.1.jar
json-smart-1.2.jar
jsr173_api-1.0.jar
jtidy-r872-jdk15.jar
junit-4.11.jar
log4j-1.2.14.jar
looks-2.2.0.jar
mail-1.4.jar
maven-plugin-api-2.0.jar
nekohtml-1.9.14.jar
not-yet-commons-ssl-0.3.11.jar
opensaml-2.5.1-1.jar
openws-1.4.2-1.jar
org.apache.oltu.oauth2.client-0.31.jar
org.apache.oltu.oauth2.common-0.31.jar
org.apache.oltu.oauth2.httpclient4-0.31.jar
out-app-analytics-provider-5.2.1.jar
proxy-vole-20131209.jar
reflections-0.9.9-RC1.jar
rsyntaxtextarea-2.5.0.jar
sac-1.3.jar
saxon-9.1.0.8j.jar
saxon-dom-9.1.0.8j.jar
serializer-2.7.1.jar
slf4j-api-1.6.1.jar
slf4j-simple-1.6.1.jar
soapui-5.2.1.jar
swingx-soapui.jar
wink-client-1.4.jar
            wink-common-1.4.jar
ws-commons-util-1.0.2.jar
wsdl4j-1.6.2-fixed.jar
wss4j-1.6.16.jar
xalan-2.7.1.jar
xbean_xpath-2.4.0.jar
xbean-fixed-2.4.0.jar
xercesImpl-2.9.1.jar
xml-apis-1.3.04.jar
xml-apis-2.9.1.jar
xmlParserAPIs-2.6.2.jar
xmlpublic-2.4.0.jar
xmlsec-1.4.5.jar
xmltooling-1.3.2-1.jar
xmlunit-1.2.jar
xom-1.1.jar
xstream-1.3.1.jar

2. Create the client configuration object

org.apache.wink.client.ClientConfig clientConfig = new org.apache.wink.client.ClientConfig();

3. Create the client object to store the configuration:

org.apache.wink.client.RestClient client = new org.apache.wink.client.RestClient(clientConfig);

4. Create the resource object setting the Service URL. Replace the <rest-url> with the actual URL to invoke the service.

org.apache.wink.client.Resource resource = client.resource(<rest-url>);

5. Invoke the service.

To call a GET service, use the following line:

org.apache.wink.client.ClientResponse resp = resource.get();

For PUT,

org.apache.wink.client.ClientResponse resp2 = resource.contentType(<media-type>).put(“<message>”);

Similarly for POST,

resource.contentType(<content-type>).accept(<media-type>).post(<message>);

6. Get the response message in String format, using

resp.getEntity(String.class)

Get the HTTP Status Code to determine if the request was successfully processed.

resp.getStatusCode()

For successful invocation, the service should return 2xx status code. For complete list of HTTP codes, please refer https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: