Jersey Rest service (1)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.

    The series of Jersey is mainly introduced how to use Jersey to build a fast RESTful service, to record their learning process in use or problems. Release of RESTful service. in the beginning will use lightweight Grizzly HTTP server

1 using Mave to create the project.
add the following since the jar in the pom.xml file, jersey-server is the realization of service side RESTful, jersey-grzzly2 is used to release the RESTful lightweight server.

 1   <dependencies>
 2       <dependency>
 3           <groupId>com.sun.jersey</groupId>
 4           <artifactId>jersey-server</artifactId>
 5           <version>1.17.1</version>
 6       </dependency>
 7       <dependency>
 8           <groupId>com.sun.jersey</groupId>
 9           <artifactId>jersey-grizzly2</artifactId>
10           <version>1.17.1</version>
11       </dependency>
12   </dependencies>

2 Server side development
1) prepared using the Annotation Root Resource Classes, Root resource Classess is a POJO object, the Annotation will be one of the methods for release of RESTful service.

 1 package com.study.jersey.server;
 2 
 3 import javax.ws.rs.GET;
 4 import javax.ws.rs.Path;
 5 import javax.ws.rs.Produces;
 6 
 7 @Path("helloworld")
 8 public class HelloWorldResource {
 9     
10     @GET
11     @Produces(MediaType.TEXT_PLAIN)
12 public String sayHelloWorld(){ 13 return "Hello World!"; 14 } 15 }

@Path:
    @The Path annotation is a relative URI path, in the code above, the Java class resource identifier can be through a UIR path /helloworld. In this category because there is only one way, so can http://<ip> <port> /hellowrld: sayHelloworld access method.
@GET:
        the annotations are defined by the Get Http resource request access to the resource, the corresponding Pot, Put etc.
@Produces:
        the annotation is used for type MIME media forms specified is the consumption of resources is returned to the client by the use of. Here the specified label is "text/plain", is the actual returns the text string information common. Various types of details will be described later.

3 for release of RESTful service

 1 package com.study.jersey.server;
 2 
 3 import java.io.IOException;
 4 import java.net.URI;
 5 
 6 import javax.ws.rs.core.UriBuilder;
 7 
 8 import org.glassfish.grizzly.http.server.HttpServer;
 9 
10 import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
11 import com.sun.jersey.api.core.PackagesResourceConfig;
12 import com.sun.jersey.api.core.ResourceConfig;
13 
14 public class PublishService {
15     
16     public static URI getBaseURI(){
17         return UriBuilder.fromUri("http://localhost/").port(9998).build();
18     }
19 
20     public static final URI BASE_URI = getBaseURI();
21     
22     protected static HttpServer startServer() throws IllegalArgumentException, NullPointerException, IOException{
23         System.out.println("Start server...");
24         ResourceConfig config = new PackagesResourceConfig("com.study.jersey.server");
25         return GrizzlyServerFactory.createHttpServer(BASE_URI, config);
26     }
27     public static void main(String[] args) {
28         try {
29             HttpServer httpServer = startServer();
30             System.out.println(String.format("Jersey app started with WADL available at" + "%sapplication.wadl\nTry out %shelloworld\nHit enter to stop it...", BASE_URI, BASE_URI));
31             System.in.read();
32             httpServer.stop();
33         } catch (IllegalArgumentException | NullPointerException | IOException e) {
34             e.printStackTrace();
35         }
36         
37     }
38 }

Executing the above code, console will prompt:

Jersey app started with WADL available athttp://localhost:9998/application.wadl
Try out http://localhost:9998/helloworld
Hit enter to stop it...

Open the browser input http://localhost:9998/helloworld, can see the return of the message "Hello World!", if you enter http://localhost:9998/application.wadl, you can see the services of XML. Here we use Grizzly to publish the service. It was the realization of the first simple RESTful services.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Sidon at November 13, 2013 - 10:31 AM