使用httpClient远程调用
第一部添加httpclient的依赖 ,添加gson的原因是httpclient远程调用返回时一个json数据,为了方便操作需要将json转换成对象
<!--远程调用--> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> </dependency> <!--json转换--> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.2</version> </dependency>
第二部编写远程调用的工具类HttpClientUtil
package com.shiwen.yitihui.achievement.util; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.utils.URIBuilder; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClientBuilder; import org.apache.http.impl.client.HttpClients; import org.apache.http.impl.client.LaxRedirectStrategy; import org.apache.http.message.BasicNameValuePair; import org.apache.http.util.EntityUtils; import java.io.IOException; import java.net.URI; import java.util.List; /** * @author : wangjie * company : 石文软件有限公司 * @date : 2020/6/9 14:27 * httpClient远程调用的工具类 */ public class HttpClientUtil { /** * 不带参数的get请求方式 */ public static String doGet(String url) { // 创建Httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); // 创建http GET请求 HttpGet httpGet = new HttpGet(url); CloseableHttpResponse response = null; try { // 执行请求 response = httpclient.execute(httpGet); // 判断返回状态是否为200 if (response.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } /** * 带参数的get请求 */ public static String doGetParam(String url, String param, String value) { // 创建Httpclient对象 CloseableHttpClient httpclient = HttpClients.createDefault(); CloseableHttpResponse response = null; try { // 定义请求的参数 URI uri = new URIBuilder(url).setParameter(param, value).build(); // 创建http GET请求 HttpGet httpGet = new HttpGet(uri); // 执行请求 response = httpclient.execute(httpGet); // 判断返回状态是否为200 if (response.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(response.getEntity(), "UTF-8"); } } catch (Exception e) { e.printStackTrace(); } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } /** * 不带参数的post请求 * @param url */ public static String doPost(String url){ // 创建Httpclient对象 CloseableHttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build(); // 创建http POST请求 HttpPost httpPost = new HttpPost(url); CloseableHttpResponse response = null; try { // 执行请求 response = httpclient.execute(httpPost); // 判断返回状态是否为200 if (response.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(response.getEntity(), "UTF-8"); } }catch(Exception e){ e.printStackTrace(); }finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } try { httpclient.close(); } catch (IOException e) { e.printStackTrace(); } } return null; } /** * * @throws Exception */ public static String doPostParam(String url,List<BasicNameValuePair> parameters) throws Exception{ // 创建Httpclient对象 CloseableHttpClient httpclient = HttpClientBuilder.create().setRedirectStrategy(new LaxRedirectStrategy()).build(); // 创建http POST请求 HttpPost httpPost = new HttpPost("http://www.oschina.net/search"); // 设置2个post参数,一个是scope、一个是q /* List<BasicNameValuePair> parameters = new ArrayList<>(); parameters.add(new BasicNameValuePair("scope", "project")); parameters.add(new BasicNameValuePair("q", "java"));*/ // 构造一个form表单式的实体 UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(parameters); // 将请求实体设置到httpPost对象中 httpPost.setEntity(formEntity); CloseableHttpResponse response = null; try { // 执行请求 response = httpclient.execute(httpPost); // 判断返回状态是否为200 if (response.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(response.getEntity(), "UTF-8"); } } finally { if (response != null) { response.close(); } httpclient.close(); } return null; } }
传入的url为了灵活一些可以直接在yml文件中配置
第三步远程调用接口
@RestController @RequestMapping("/auth") public class TemplateAuthController { //调用server层用户的地址 @Value("${httpclient.address}") private String url; private Gson gson = new Gson(); @RequestMapping("/http-client") public User getServerUser() { //远程调用获取server服务的接口数据 String userJson = HttpClientUtil.doGetParam(url, "userName", UserUtil.getCurrUsername()); if (userJson != null) { //json转换成user对象 User user = gson.fromJson(userJson, User.class); return user; } return null; } }