关于程序员:jtday17

3次阅读

共计 3357 个字符,预计需要花费 9 分钟才能阅读完成。

1.HttpClient

1.HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的反对 HTTP 协定的客户端编程工具包
业务需要阐明

  1. HttpClient 入门案例
    1)### 导入 jar 包
    2)### HttpClient 入门案例
package com.jt.test;
import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.IOException;
//@SpringBootTest   // 从 spring 容器中获取 bean 对象, 之后实现测试业务.
public class TestHttpClient {
    /**
     * 1. 实例化 HttpClient 对象
     * 2. 定义近程拜访的 url 地址
     * 3. 定义申请类型的对象
     * 4. 发动 http 申请, 获取响应的后果
     * 5. 对返回值后果进行校验. 获取实在的数据信息.
     * */
    @Test
    public void testGet() throws IOException {HttpClient httpClient = HttpClients.createDefault();
       String url = "http://www.baidu.com";
       HttpGet httpGet = new HttpGet(url);
       HttpResponse httpResponse = httpClient.execute(httpGet);
       // 常见后果状态 200 404 406 参数异样  500 后端服务器异样 504 超时  502 拜访的网址不存在
        // 获取状态码
        int status = httpResponse.getStatusLine().getStatusCode();
        if(status == 200){
            // 获取响应后果
            HttpEntity entity = httpResponse.getEntity();
            String result = EntityUtils.toString(entity,"UTF-8");
            System.out.println(result);
        }
    }
}

3.HttpClient 实现业务逻辑(前端【controller–service】– 后端【controller–service】)
1)### 业务需要
用户通过 http://www.jt.com/user/testHt…
JT-WEB 服务器 拜访 JT-SSO 时的申请 http://sso.jt.com/user/testHt…
2)### 编辑前端 Controller

@RequestMapping("/testHttpClient")
    @ResponseBody
    public List<User> testHttpClient(){return userService.testHttpClient();
    }

3) ### 编辑前端 Service

package com.jt.service;
import com.jt.pojo.User;
import com.jt.util.ObjectMapperUtil;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
    @Override
    public List<User> testHttpClient() {List userList = new ArrayList<>();
        // 由 jt-web 服务器去链接 jt-sso 的服务器
        String url = "http://sso.jt.com/user/testHttpClient";
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        try {HttpResponse httpResponse =httpClient.execute(httpGet);
            if(httpResponse.getStatusLine().getStatusCode() == 200){HttpEntity httpEntity = httpResponse.getEntity();
            String result = EntityUtils.toString(httpEntity, "UTF-8");
            userList = ObjectMapperUtil.toObject(result, userList.getClass());
           /* for (LinkedHashMap<String,Object> map : userList){User userTemp = new User();
                    userTemp.setId(Long.parseLong(map.get("id")+""));
                    userTemp.setUsername((String)map.get("username"));
                    userTemp.setPassword((String)map.get("password"));
                    userTemp.setPhone((String)map.get("phone"));
                    userTemp.setEmail((String)map.get("phone"));
                    userList2.add(userTemp);
                }*/
            }
        } catch (IOException e) {e.printStackTrace();
            throw new RuntimeException(e);
        }
        return userList;
    }
}

4) ### 编辑后端 Controller

/**
     * http://sso.jt.com/user/testHttpClient
     * 返回 List<User>
     */
    @RequestMapping("testHttpClient")
    public List<User> testHttpClient(){return userService.findAll();
    }

5) ### 编辑后端 Service

@Override
    public List<User> findAll() {return userMapper.selectList(null);
    }
正文完
 0