easy, elegant and effective code…

This example will show you how to write a java client to upload a file on Jersey RESTful Web Service (In my earlier post I have given an example to write a Jersey RESTful Web Service).

Consider that a Jersey RESTful Web Service is up and running. WADL URL is -
http://localhost:8585/RESTFileUpload/rest/application.wadl’ and content looks like as follows -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<application xmlns="http://wadl.dev.java.net/2009/02">
	<doc xmlns:jersey="http://jersey.java.net/" jersey:generatedBy="Jersey: 1.12 02/15/2012 05:30 PM" />
	<grammars />
	<resources base="http://localhost:8585/RESTFileUpload/rest/">
		<resource path="/upload">
			<resource path="/file">
				<method name="POST" id="fileUpload">
					<request>
						<representation mediaType="multipart/form-data" />
					</request>
					<response>
						<representation mediaType="*/*" />
					</response>
				</method>
			</resource>
		</resource>
	</resources>
</application>

This client code will try to upload a file – ‘TestUpload.ear’ from the location – ‘D:/ClientExample’ by accessing the service through above mentioned URL. On completion, it will display the response code received from the server. In case of successful upload, OK response will be returned.

Java Client Consume Above Mentioned RESTful Web Service

package com.crispCode.rest.client;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;

import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.multipart.FormDataMultiPart;
import com.sun.jersey.multipart.file.FileDataBodyPart;

public class FileUploadClient 
{
	private static URI getBaseURI()
	{
		return UriBuilder.fromUri("http://localhost:8585/RESTFileUpload/rest")
	}

	public static void main(String[] args) throws FileNotFoundException 
	{
		final ClientConfig config = new DefaultClientConfig();
		final Client client = Client.create(config);

		final WebResource resource = client.resource(getBaseURI())
				.path("upload").path("file");

		final File fileToUpload = new File("D:/ClientExample/TestUpload.ear");

		final FormDataMultiPart multiPart = new FormDataMultiPart();
		if (fileToUpload != null) 
		{
			multiPart.bodyPart(new FileDataBodyPart("file", fileToUpload,
					MediaType.APPLICATION_OCTET_STREAM_TYPE));
		}

		final ClientResponse clientResp = resource.type(
				MediaType.MULTIPART_FORM_DATA_TYPE).post(ClientResponse.class,
				multiPart);
		System.out.println("Response: " + clientResp.getClientResponseStatus());

		client.destroy();
	}
}

Code Description
Line #27-28: Creating an instance of a Client To utilize the client API using config.
Line #30: Once a client instance is created and configured, obtain a WebResource instance.
Line #35: For client side application wishing to construct an outgoing multipart/form-data message, instance of FormDataMultiPart is required to be created.
Line #42-44: multipart/form-data is posted and the response is received in ClientResponse object.
Line #45: Displaying the response status code received from the server.
Line #47: calling destroy() on the Client instance to cleanup any resource associated with the client. (NOTE: This method must be called when there are not responses pending otherwise undefined behavior will occur. The client must not be reused after this method is called otherwise undefined behavior will occur.)

Required Jar Files
Following is the list of jar files required for the successful compilation and execution of the client code -
jersey-bundle-1.12.jar
jersey-core-1.12.jar
jersey-multipart-1.12.jar

References
http://jersey.java.net/nonav/documentation/latest/client-api.html

About these ads

Leave a Reply

Please log in using one of these methods to post your comment:

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: