一、调用者
1、创建JavaWeb工程
2、创建lib目录,并引入相应的jar包
# 创建lib目录:web/WEB-INF/lib,并引入如下jar包
httpclient-4.5.2.jar
lombok-1.18.20.jar
commons-lang-2.3.jar
commons-logging-1.1.1.jar
3、创建config.properties文件
# 路径:web/WEB-INF/config/config.properties,内容如下
test.query=http://localhost:8091/test/query
test.test=http://localhost:8091/test/test
4、修改端口号
5、创建工具类
(1)创建HttpClient的工具类
public class HttpUtil {
/*httpClient的post请求*/
public String post(String url,Map map){
String result=null;
// 创建 HttpClient 客户端
CloseableHttpClient httpClient = HttpClients.createDefault();
// 创建 HttpPost 请求
HttpPost httpPost = new HttpPost(url);
String json = JSON.toJSONString(map);
CloseableHttpResponse httpResponse = null;
try {
httpPost.addHeader("Content-type","application/json;chartset=utf-8");
httpPost.setHeader("Accept","application/json");
// 创建请求内容
StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
httpPost.setEntity(entity);
httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// 输出请求结果
result=EntityUtils.toString(httpEntity,Charset.forName("utf-8"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// 无论如何必须关闭连接
finally {
try {
if (httpResponse != null) {
httpResponse.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (httpClient != null) {
httpClient.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
/*httpClient的get请求*/
public String get(String url,List<NameValuePair> list){
String result=null;
CloseableHttpClient httpclient = HttpClients.createDefault();
CloseableHttpResponse response = null;
try {
URIBuilder uriBuilder = new URIBuilder(url);
uriBuilder.setParameters(list);
HttpGet httpGet = new HttpGet(uriBuilder.build());
// 执行http get请求
response = httpclient.execute(httpGet);
// 判断返回状态是否为200
if (response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity(), Charset.forName("utf-8"));
}
} catch (URISyntaxException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
if (response != null) {
response.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}
}
(2)创建动态获取config.properties上参数的工具类
package com.liws.test.tool;
public class ReadConfigFileInfo {
public String getValFromConfig(String key){
try{
String url = this.getClass().getResource("").getPath().replaceAll("%20", " ");
String path = url.substring(0, url.indexOf("WEB-INF")) + "WEB-INF/config/config.properties";
Properties config = new Properties();
config.load(new FileInputStream(path));
return config.getProperty(key);
}
catch(Exception e){
e.printStackTrace();
}
return null;
}
}
6、创建响应实体类
package com.liws.test.model;
@Data
public class TestInfo implements Serializable {
private static final long serialVersionUID = 1L;
private String organizationName;
private String organizationType;
private String businessType;
}
7、创建测试类
package com.liws.test.controller;
public class HttpClientTest{
public static void main(String[] args) {
//testHttpClientPost();
testHttpClientGet();
}
//测试httpClient的post请求
public static void testHttpClientPost(){
ReadConfigFileInfo read = new ReadConfigFileInfo();
String url = read.getValFromConfig("test.query");
Map<String,String> map=new HashMap<>();
map.put("organizationName","小明");
HttpUtil httpUtil = new HttpUtil();
String result = httpUtil.post(url, map);
JSONArray array = JSON.parseArray(result);
List<TestInfo> testInfoList = array.toJavaList(TestInfo.class);
System.out.println(testInfoList);
}
//测试httpClient的Get请求
public static void testHttpClientGet(){
ReadConfigFileInfo read = new ReadConfigFileInfo();
String url = read.getValFromConfig("test.test");
List<NameValuePair> list = new LinkedList<>();
list.add(new BasicNameValuePair("organizationName", "小明"));
HttpUtil httpUtil = new HttpUtil();
String result = httpUtil.get(url, list);
JSONArray array = JSON.parseArray(result);
List<TestInfo> testInfoList = array.toJavaList(TestInfo.class);
System.out.println(testInfoList);
}
}
二、被调用者
被调用者,即服务端,其提供restful风格接口以供调用者(客户端)来调用。
创建被调用者,可以是任意一个springboot的项目。