其他分享
首页 > 其他分享> > 使用Http客户端工具实现Json数据与对象的转换

使用Http客户端工具实现Json数据与对象的转换

作者:互联网

1、创建HttpGet请求,在请求里传入url。

  HttpGet request = new HttpGet("http://localhost:xx/xx");

2.获取Json数据。使用CloseableHttpClient 对象,调用其execute()方法,传入HttpGet对象和BasicResponseHandler对象。

  String response = this.httpClient.execute(request, new BiBasicResponseHandler());

3.使用ObjectMapper对象处理Json。

  3.1 简单转换(转换不了集合类型):

    使用ObjectMapper对象,调用其readValue()方法,传入Json数据和需要转换的类的字节码对象。

    User user = mapper.readValue(response, User.class);

  3.2 复杂转换(处理复杂类型List<User>):

    使用ObjectMappper对象获取类型工厂getFactory(),并调用构造集合类型函数constructCollectionType("集合类型","目标类"),分别传入集合和目标类的字节码对象。

    使用ObjectMapper对象,调用其readValue()方法,传入Json数据和构造集合类型。

    List<User> users = mapper.readValue(response, mapper.getFactory().constructCollectionType(List.Class, User.class));

  3.3 万能转换(能处理一切类型,包括集合中嵌套集合的类型):

    使用ObjectMapper对象调用其readValue()方法,传入Json数据和TypeReference<List<User>>()对象并指定泛型。

    List<User> users = mapper.readValue(response, new TypeReference<List<User>>() { });  

 

代码:

public class HttpTests {

            CloseableHttpClient httpClient;

    // 处理json
   
ObjectMapper mapper = new ObjectMapper();

    @Before
    public void init() {
        // 创建httpClient的客户端
       
httpClient = HttpClients.createDefault();
    }

    @Test
    public void testGet() throws IOException {
        // 发起get请求
       
HttpGet request = new HttpGet("http://www.baidu.com");
        // 执行请求
       
String response = this.httpClient.execute(request, new BasicResponseHandler());
        System.out.println(response);
    }

    @Test
    public void testPost() throws IOException {
       /* HttpGet request = new HttpGet("http://www.oschina.net/");*/
       
HttpPost request = new HttpPost("http://www.oschina.net/");
        request.setHeader("User-Agent",
                "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.72 Safari/537.36");
        String response = this.httpClient.execute(request, new BasicResponseHandler());
        System.out.println(response);
    }

    @Test
    public void testGetPojo() throws IOException {
        HttpGet request = new HttpGet("http://localhost:33/list");
        // 获取JSON数据
       
String response = this.httpClient.execute(request, new BasicResponseHandler());
        // 处理JSON,转简单类型,集合类型转不了
       
/*User user = mapper.readValue(response, User.class);*/

        //
处理复杂类型List<User>
        //
使用ObjectMapper对象获取类型工厂getTypeFactory()
        //List<User> users = mapper.readValue(response, mapper.getTypeFactory().constructCollectionType(List.class, User.class));
        //
处理一切类型,包括集合中嵌套集合的类型
       
List<User> users = mapper.readValue(response, new TypeReference<List<User>>() {});
        for (User user: users) {
            System.out.println(user);
        }
        // System.out.println(users);
   
}
}

POM依赖文件:

 

<name>http-demo</name>
<description>Demo project for Spring Boot</description>

<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.0.1.RELEASE</version>
   <relativePath/><!-- lookup parent from repository -->
</parent>

<properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
   <java.version>1.8</java.version>
</properties>

<dependencies>
   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter</artifactId>
   </dependency>

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
   </dependency>

   <dependency>
      <groupId>org.apache.httpcomponents</groupId>
      <artifactId>httpclient</artifactId>
      <version>4.5.1</version>
   </dependency>

   <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
   </dependency>
</dependencies>

<build>
   <plugins>
      <plugin>
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
   </plugins>
</build>

 

 

 

使用ObjectMapper对象获取类型工厂getTypeFactory()并调用构造集合类型函数constructCollectionType(“集合类型”,”目标类”),分别传入集合和目标类的字节码对象。
使用ObjectMapper对象,调用其readValue()方法,传入Json数据和构造好的集合类型。

标签:mapper,HttpGet,Http,readValue,request,Json,new,response,客户端
来源: https://www.cnblogs.com/sfwu/p/16489345.html