Jersey客户端

Jersey 很适合编写函数式风格的客户端,本篇博客是在上一篇博客的基础上来实现Jersey客户端。即需要上篇博客中cxf-rs-spring为客户端提供Web服务。

本篇博客实现了GET、PUT以及POST请求,同时实现了如何向服务端发送请求数据以及接受并读取来自服务端的数据。


一、使用maven创建java项目jersey_client

Jersey客户端_第1张图片


二、配置pom.xml 添加依赖


  4.0.0

  demo.jersey
  jersey_client
  0.0.1-SNAPSHOT
  jar

  jersey_client
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      3.8.1
      test
    
    
    
    
		org.glassfish.jersey.core
		jersey-client 2.22.2
	
	
  
  
	  
          	
                     org.codehaus.mojo
               	     exec-maven-plugin
                     1.1
                     
                  	java
                     
                     
                  	demo.jersey.jersey_client.App
                     
          	
           
	


三、在/src/main/resources/目录下(没有就新建)添加资源文件

 add_customer.xml



    Jack


update_customer.xml



    Mary
    123

四、 在原根资源的基础上,增加两个服务

@GET
    @Path("/{username}/girlfriend")
    public String girlFriend(@PathParam("username") String userName, @QueryParam("")Customer user) {
            return "hello " + userName + ": your's gf is " + user.getName() + ",whose id is " + user.getId();
    }
    
    @POST
    @Path("/{username}/girlfriend")
    public String girlFriendPost(@PathParam("username") String userName,
                    @FormParam("") Customer user ) {
            return "hello " + userName + ": your's gf is " + user.getName() + ",whose id is " + user.getId();
    }

五、编写客户端代码

package demo.jersey.jersey_client;

import java.io.File;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;



public class App 
{
    public static void main( String[] args ) { 
       Client client = ClientBuilder.newClient();
       
       // Sent HTTP GET request to query customer info
       System.out.println("Sent HTTP GET request to query customer info");
       WebTarget target = client.target("http://localhost:8080/service1/customerservice").path("customers/123");
       String s = target.request(MediaType.TEXT_XML_TYPE).get(String.class);
       System.out.println(s);
       System.out.println("\n");
       
       // Sent HTTP GET request to query sub resource product info
       target = client.target("http://localhost:8080/service1/customerservice").path("orders/223/products/323");
       s = target.request(MediaType.APPLICATION_JSON_TYPE).get(String.class);
       System.out.println(s);
       System.out.println("\n");
       
       target = client.target("http://localhost:8080/service1/customerservice").path("jack/girlfriend");
       s = target.queryParam("name", "mary").queryParam("id", 1433).request(MediaType.TEXT_PLAIN_TYPE).get(String.class);
       System.out.println(s);
       System.out.println("\n");
       
       App app = new App();
       // Sent HTTP PUT request to update customer info
       System.out.println("Sent HTTP PUT request to update customer info");
       String inputFile = app.getClass().getResource("/update_customer.xml").getFile();
       File input = new File(inputFile);
       target = client.target("http://localhost:8080/service1/customerservice").path("customers");
       s = target.request(MediaType.APPLICATION_JSON_TYPE).put(Entity.entity(input, MediaType.TEXT_XML_TYPE),String.class);
       System.out.println(s);
       System.out.println("\n");
       
       // Sent HTTP POST request to add customer
       System.out.println("Sent HTTP POST request to add customer");
       inputFile = app.getClass().getResource("/add_customer.xml").getFile();
       input = new File(inputFile);
       
       Response response = target.request(MediaType.TEXT_XML_TYPE).post(Entity.entity(input, MediaType.TEXT_XML_TYPE));
       System.out.println(response.getStatus());
       System.out.println(response.readEntity(String.class));
       System.out.println("\n");
       
       Form form = new Form();
       form.param("name", "Nancy");
       form.param("id", "1333");
       
       target = client.target("http://localhost:8080/service1/customerservice").path("michal/girlfriend");
       s = target.request(MediaType.TEXT_PLAIN_TYPE).post(Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE),String.class);
       System.out.println(s);
       System.out.println("\n");
    }
    
}

文件目录结构:

Jersey客户端_第2张图片


六、运行项目,maven运行参数:  install exec:java

运行结果:

Jersey客户端_第3张图片


参考链接:

https://jersey.java.net/documentation/latest/client.html

你可能感兴趣的:(分布式计算)