Thursday, July 26, 2012

JAX-RS JUG Demo Examples (@QueryParam and @PathParam)

I gave a demo of JAX-RS using @QueryParam and @PathParam annotations, and created a client to connect with the resources and display the results. I promised to post the code. So here it is along with some cleanup.

Here are the Apache Maven projects and code:
simple-queryparam-example.zip
simple-queryparam-client-example.zip

Example.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.bluelotussoftware.example;
 
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.QueryParam;
 
/**
 *
 * @author John Yeary
 * @version 1.0
 */
@Path("example")
public class Example {
 
    @GET
    public String helloQueryParam(@QueryParam("name") String name) {
        if (name != null) {
            return "Hello, " + name;
        } else {
            return "Hello Anonymous";
        }
    }
 
    @GET
    @Path("{name}")
    public String helloPathParam(@PathParam("name") String name) {
        if (name != null) {
            return "Hello, " + name;
        } else {
            return "Hello Anonymous";
        }
    }
}

SimpleExampleClientApplication.java


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.bluelotussoftware.example;
 
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
 
/**
 * Simple Example Client Application
 *
 * @author John Yeary
 * @version 1.0
 */
public class SimpleExampleClientApplication {
     
    public static void main(String[] args) {
        ExampleClient ec = new ExampleClient();
        System.out.println(ec.helloQueryParam("Swathi"));
        System.out.println(ec.helloPathParam("John"));
        ec.close();
    }
     
    static class ExampleClient {
         
        private WebResource webResource;
        private Client client;
        private static final String BASE_URI = "http://localhost:8080/simple-queryparam-example/resources";
         
        public ExampleClient() {
            com.sun.jersey.api.client.config.ClientConfig config = new com.sun.jersey.api.client.config.DefaultClientConfig();
            client = Client.create(config);
            webResource = client.resource(BASE_URI).path("example");
        }
         
        public String helloQueryParam(String name) throws UniformInterfaceException {
            WebResource resource = webResource;
            if (name != null) {
                resource = resource.queryParam("name", name);
            }
            return resource.get(String.class);
        }
         
        public String helloPathParam(String name) throws UniformInterfaceException {
            WebResource resource = webResource;
            resource = resource.path(java.text.MessageFormat.format("{0}", new Object[]{name}));
            return resource.get(String.class);
        }
         
        public void close() {
            client.destroy();
        }
    }
}

0 comments :

Popular Posts