Jersey实现Rest service(1)

    本系列的Jersey主要是快速介绍如何使用Jersey建立RESTful service,记录自己在学习过程中使用或遇到的问题。在最开始会使用轻量级的Grizzly HTTP server发布RESTful service.

1. 使用Mave创建工程
在pom.xml文件中加入如下以来的jar, jersey-server是实现service side的RESTful, jersey-grzzly2是用来发布RESTful的轻量级的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的开发
1) 使用Annotation编写Root Resource Classes, Root resource Classess实际是一个POJO对象,通过Annotation将其中的方法发布为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:
    @Path注释是一个相对的URI路径,在上面的代码中,Java class的资源标识可以通过UIR路径/helloworld表示。在此类中因为只有一个方法,所以可以通过http://<ip>:<port>/hellowrld访问sayHelloworld方法。
@GET:
     该注释是定义资源通过Http的Get请求方式可以访问该资源,相应的还有Pot, Put等。
@Produces:
     该注释是用于指定被消费的资源返回到客户端所使用的MIME媒体表现方式的类型。在这里指定的标示是"text/plain",实际就是返回普通的文本字符串信息。详细的各种类型会在后面介绍。

3. 发布为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 }

执行上面代码,在console中会提示:

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

打开浏览器输入http://localhost:9998/helloworld,可看到返回信息"Hello World!",如果输入 http://localhost:9998/application.wadl,可以看到服务的xml描述. 在这里我们使用Grizzly将服务发布出来。这就实现了第一个简单的RESTful服务。

原文地址:https://www.cnblogs.com/newstar/p/3112086.html