Jersey客户端

来源:互联网 发布:java condition 编辑:程序博客网 时间:2024/06/04 01:10

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

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


一、使用maven创建java项目jersey_client



二、配置pom.xml 添加依赖

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>demo.jersey</groupId>  <artifactId>jersey_client</artifactId>  <version>0.0.1-SNAPSHOT</version>  <packaging>jar</packaging>  <name>jersey_client</name>  <url>http://maven.apache.org</url>  <properties>    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <dependencies>    <dependency>      <groupId>junit</groupId>      <artifactId>junit</artifactId>      <version>3.8.1</version>      <scope>test</scope>    </dependency>        <!-- Jersey dependencies -->    <dependency><groupId>org.glassfish.jersey.core</groupId><artifactId>jersey-client</artifactId> <version>2.22.2</version></dependency>  </dependencies>  <build>  <plugins>          <plugin>                     <groupId>org.codehaus.mojo</groupId>                    <artifactId>exec-maven-plugin</artifactId>                     <version>1.1</version>                     <executions>                  <execution><goals><goal>java</goal></goals></execution>                     </executions>                     <configuration>                  <mainClass>demo.jersey.jersey_client.App</mainClass>                     </configuration>          </plugin>           </plugins></build></project>

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

 add_customer.xml

<?xml version="1.0"?><Customer>    <name>Jack</name></Customer>

update_customer.xml

<?xml version="1.0"?><Customer>    <name>Mary</name>    <id>123</id></Customer>

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

@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");    }    }

文件目录结构:



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

运行结果:


参考链接:

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

0 0