Jersey的RESTful简单案例demo

REST基础概念:

  • 在REST中的一切都被认为是一种资源。
  • 每个资源由URI标识。
  • 使用统一的接口。处理资源使用POST,GET,PUT,DELETE操作类似创建,读取,更新和删除(CRUD)操作。
  • 无状态。每个请求是一个独立的请求。从客户端到服务器的每个请求都必须包含所有必要的信息,以便于理解。
  • 通信都是通过展现。例如XML,JSON

RESTful Web服务由于其简单替代了基于SOAP的Web服务,并大型服务提供商所接受。这篇文章使用Jersey框架延伸JAX-RS API将展示如何创建一个REST风格的Web服务和客户端。

 

在Eclipse中,创建一个新的动态Web项目名为"RESTfulWS":

 

下载Jersey zip bundle 这里 here. 需要包:

  1. asm-3.1.jar
  2. jersey-client-1.17.1.jar
  3. jersey-core-1.17.1.jar
  4. jersey-server-1.17.1.jar
  5. jersey-servlet-1.17.1.jar
  6. jsr311-api-1.1.1.jar

jersey

加入项目:

 创建Web服务类:

package com.snow.jersey;

import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * 
 * @author pavithra
 * 
 */

// @Path here defines class level path. Identifies the URI path that 
// a resource class will serve requests for.
@Path("UserInfoService")
public class UserInfo {
    
     // @GET here defines, this method will method will process HTTP GET
     // requests.
     @GET
     // @Path here defines method level path. Identifies the URI path that a
     // resource class method will serve requests for.
     @Path("/name/{i}")
     // @Produces here defines the media type(s) that the methods
     // of a resource class can produce.
     @Produces(MediaType.TEXT_XML)
     // @PathParam injects the value of URI parameter that defined in @Path
     // expression, into the method.
     public String userName(@PathParam("i") String i) {
    
      String name = i;
      return "<User>" + "<Name>" + name + "</Name>" + "</User>";
     }
    
     @POST
     @Path("/age/{j}") 
     @Produces(MediaType.TEXT_XML)
     public String userAge(@PathParam("j") int j) {
    
      int age = j;
      return "<User>" + "<Age>" + age + "</Age>" + "</User>";
     }
}

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
      <display-name>RESTfulWS</display-name>  
      <servlet>  
            <servlet-name>Jersey REST Service</servlet-name>  
            <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>  
            <init-param>  
                  <param-name>com.sun.jersey.config.property.packages</param-name>  
                  <param-value>com.snow.jersey</param-value>  
            </init-param>  
            <load-on-startup>1</load-on-startup>  
      </servlet>  
      <servlet-mapping>  
            <servlet-name>Jersey REST Service</servlet-name>  
            <url-pattern>/rest/*</url-pattern>  
      </servlet-mapping>   
</web-app>

 

点按这个项目 run as ->run on server.

浏览器浏览:http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra

URL说明:

 创建一个调用客户端:

package com.snow.jersey;

import javax.ws.rs.core.MediaType;

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;

/**
 * 
 * @author pavithra
 * 
 */
public class UserInfoClient {

     public static final String BASE_URI = "http://localhost:8080/jersey";
     public static final String PATH_NAME = "/UserInfoService/name/";
     public static final String PATH_AGE = "/UserInfoService/age/";
    
     public static void main(String[] args) {
             
          String name = "Pavithra";
          int age = 25;
        
          ClientConfig config = new DefaultClientConfig();
          Client client = Client.create(config);
          WebResource resource = client.resource(BASE_URI);
        
          WebResource nameResource = resource.path("rest").path(PATH_NAME + name);
          System.out.println("Client Response \n"  + getClientResponse(nameResource));
          System.out.println("Response \n" + getResponse(nameResource) + "\n\n");
        
          WebResource ageResource = resource.path("rest").path(PATH_AGE + age);
          System.out.println("Client Response \n" + getClientResponse(ageResource));
          System.out.println("Response \n" + getResponse(ageResource));
}
/** * Returns client response. * e.g : * GET http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra * returned a response status of 200 OK * * @param service * @return */ private static String getClientResponse(WebResource resource) { return resource.accept(MediaType.TEXT_XML).get(ClientResponse.class).toString(); } /** * Returns the response as XML * e.g : <User><Name>Pavithra</Name></User> * * @param service * @return */ private static String getResponse(WebResource resource) { return resource.accept(MediaType.TEXT_XML).get(String.class); } }

 

public  void testHelloService() throws URISyntaxException {  
    Client client = Client.create();  
    URI u = new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello");  
    System.out.println(u);  
    WebResource resource = client.resource(u);  
    //get  
    String result = resource.get(String.class);  
    System.out.println(result);  
      
    //get param  
    u = new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/sex");  
    System.out.println(u);  
    resource = client.resource(u);  
    MultivaluedMapImpl params = new MultivaluedMapImpl();  
    params.add("name", "houfeng");  
    result = resource.queryParams(params).get(String.class);  
    System.out.println(result);  
      
    u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_get");  
    System.out.println(u);  
    resource = client.resource(u);  
    params = new MultivaluedMapImpl();  
    params.add("name", "houfeng");  
    result = resource.queryParams(params).get(String.class);  
    System.out.println(result);  
      
    u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_get2");  
    System.out.println(u);  
    resource = client.resource(u);  
    params = new MultivaluedMapImpl();  
    params.add("name", "houfeng");  
    result = resource.queryParams(params).get(String.class);  
    System.out.println(result);  
  
      
    u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post1");  
    System.out.println(u);  
    resource = client.resource(u);  
    params = new MultivaluedMapImpl();  
    params.add("name", "houfeng");  
    result = resource.type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class,params);  
    System.out.println(result);  
      
  //post u
=new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post2"); System.out.println(u); resource = client.resource(u); params = new MultivaluedMapImpl(); params.add("name", "houfeng"); result = resource.queryParams(params).type(MediaType.APPLICATION_FORM_URLENCODED).post(String.class); System.out.println(result); u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post3"); System.out.println(u); resource = client.resource(u); result = resource.entity("hello").post(String.class); System.out.println(result); u =new URI("http://houfeng:8080/jerseyWebServiceTest/services/hello/test_post4"); System.out.println(u); resource = client.resource(u); String buf = "inputstream content."; ByteArrayInputStream bais = new ByteArrayInputStream(buf.getBytes()); result = resource.entity(bais).post(String.class); System.out.println(result); }

 

运行这个客户端代码,得到结果:

Client Response 
GET http://localhost:8080/RESTfulWS/rest/UserInfoService/name/Pavithra returned a response status of 200 OK
Response 
<User><Name>Pavithra</Name></User>

Client Response 
GET http://localhost:8080/RESTfulWS/rest/UserInfoService/age/25 returned a response status of 200 OK
Response 
<User><Age>25</Age></User>

 

posted @ 2016-06-20 10:30  龙昊雪  阅读(6169)  评论(0编辑  收藏  举报