1.HttpClient
1.HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包
业务需求说明
- 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);
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。