Wednesday November 26, 2008
TOTD #57: Jersey Client API - simple and easy to use
TOTD
#56 explains how to create a RESTful Web service endpoint
using Jersey
and publish the resource using JSON representation. The blog entry
showed how the endpoint can be accessed from a Web browser. This Tip Of The Day explains how to
use Jersey
Client APIs to invoke the published endpoint.
Lets get started!
| package org.glassfish.samples; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * Unit test for simple App. */ public class AppTest extends TestCase { /** * Create the test case * * @param testName name of the test case */ public AppTest( String testName ) { super( testName ); } /** * @return the suite of tests being tested */ public static Test suite() { return new TestSuite( AppTest.class ); } /** * Rigourous Test :-) */ public void testApp() { assertTrue(true); } } |
| private
WebResource createResource() { Client client = Client.create(); WebResource resource = client.resource("http://localhost:8080/helloworld-webapp/webresources/myresource"); return resource; } |
|
Greeting result = createResource().get(Greeting.class); assertTrue(result.greeting.equals("Hi there!")); |
| import com.sun.jersey.api.client.Client; import com.sun.jersey.api.client.WebResource; |
| ~/samples/jersey/helloworld-webapp >mvn test [INFO] Scanning for projects... [INFO] ------------------------------------------------------------------------ [INFO] Building helloworld-webapp Jersey Webapp [INFO] task-segment: [test] [INFO] ------------------------------------------------------------------------ [INFO] [resources:resources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:compile] [INFO] Nothing to compile - all classes are up to date [INFO] [resources:testResources] [INFO] Using default encoding to copy filtered resources. [INFO] [compiler:testCompile] [INFO] Compiling 1 source file to /Users/arungupta/samples/jersey/helloworld-webapp/target/test-classes [INFO] [surefire:test] [INFO] Surefire report directory: /Users/arungupta/samples/jersey/helloworld-webapp/target/surefire-reports ------------------------------------------------------- T E S T S ------------------------------------------------------- Running org.glassfish.samples.AppTest Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.587 sec Results : Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESSFUL [INFO] ------------------------------------------------------------------------ [INFO] Total time: 4 seconds [INFO] Finished at: Mon Nov 24 16:50:17 PST 2008 [INFO] Final Memory: 18M/43M [INFO] ------------------------------------------------------------------------ |
|
Client client = Client.create(); WebResource resource = client.resource("http://localhost:8080/helloworld-webapp/webresources/myresource"); resource.addFilter(new LoggingFilter()); return resource; |
| Running org.glassfish.samples.AppTest 1 * Out-bound request 1 > GET http://localhost:8080/helloworld-webapp/webresources/myresource 1 > 1 < 200 1 < X-Powered-By: Servlet/2.5 1 < Transfer-Encoding: chunked 1 < Content-Type: application/json 1 < Server: GlassFish/v3 1 < Date: Tue, 25 Nov 2008 07:07:51 GMT 1 < {"greeting":"Hi there!"} 1 * In-bound response Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 1.074 sec |
Posted by Arun Gupta in Finance | Comments[11]
|
|
|
|
|
Today's Page Hits: 3297
Total # blog entries: 994