使用HttpClient工具类发起Restful API调用
1 import com.alibaba.fastjson.JSONObject; 2 import org.apache.http.HttpResponse; 3 import org.apache.http.NameValuePair; 4 import org.apache.http.client.ClientProtocolException; 5 import org.apache.http.client.HttpClient; 6 import org.apache.http.client.entity.UrlEncodedFormEntity; 7 import org.apache.http.client.methods.HttpGet; 8 import org.apache.http.client.methods.HttpPost; 9 import org.apache.http.impl.client.HttpClients; 10 import org.apache.http.message.BasicNameValuePair; 11 import org.apache.http.util.EntityUtils; 12 import java.io.IOException; 13 import java.util.ArrayList; 14 import java.util.List; 15 import java.util.Map; 16 17 public class ClientUtils { 18 19 /** 20 * POST 请求 21 * @param url 请求地址 22 * @param params 请求参数 23 * @param encode 编码格式 24 * @return 25 * @throws ClientProtocolException 26 * @throws IOException 27 */ 28 public String doPostMethod(String url, Map<String,String> params, String encode) throws ClientProtocolException, IOException { 29 HttpClient httpclient = HttpClients.createDefault(); 30 HttpPost httpPost = new HttpPost(url); 31 if (params != null) { 32 List<NameValuePair> form = new ArrayList<NameValuePair>(); 33 for (String name : params.keySet()) { 34 form.add(new BasicNameValuePair(name,params.get(name))); 35 } 36 UrlEncodedFormEntity entity = new UrlEncodedFormEntity(form,encode); 37 httpPost.setEntity(entity); 38 //设置请求头 39 httpPost.setHeader("Accept","text/plain;charset=utf-8"); 40 httpPost.setHeader("Cache-Control","no-cache"); 41 } 42 HttpResponse response = httpclient.execute(httpPost); 43 String result = EntityUtils.toString(response.getEntity()); 44 return result; 45 } 46 47 /** 48 * GET 49 * @param url 50 * @param params 51 * @param encode 52 * @return 53 * @throws ClientProtocolException 54 * @throws IOException 55 */ 56 public JSONObject doGetMethod(String url,List<NameValuePair> params,String encode) throws ClientProtocolException, IOException{ 57 HttpClient httpclient = HttpClients.createDefault(); 58 //参数转换为字符串 59 String paramsStr = EntityUtils.toString(new UrlEncodedFormEntity(params, encode)); 60 url = url + "?" + paramsStr; 61 // 创建httpget. 62 HttpGet httpget = new HttpGet(url); 63 System.out.println("executing request " + httpget.getURI()); 64 HttpResponse response = httpclient.execute(httpget); 65 String result = EntityUtils.toString(response.getEntity()); 66 JSONObject obj = JSONObject.parseObject(result); 67 return obj; 68 } 69 70 } 71 72 @ResponseBody 73 @RequestMapping(value = "/exportVehicleRelation", method = RequestMethod.POST) 74 @ApiOperation(value = "车辆相关导出", notes = "车辆相关导出") 75 public List<ExportVehicleRelationDto> exportVehicleList(@ModelAttribute VehicleListQuery vehicleListQuery) throws IOException { 76 LoginInfoService loginSessionBean = this.loginInfoService; 77 Map<String,String> params = new HashMap<>(); 78 params.put("bindType","2"); 79 params.put("pageNo", vehicleListQuery.getPageNo().toString()); 80 params.put("pageSize", vehicleListQuery.getPageSize() == 10 ? "500" : vehicleListQuery.getPageSize().toString()); 81 params.put("appIds", StringUtils.isEmpty(vehicleListQuery.getAppIds())?deliveryHomeVehicleAppId:vehicleListQuery.getAppIds().toString()); 82 params.put("vehicleNumber", vehicleListQuery.getVehicleNumber()); 83 params.put("vehicleType", vehicleListQuery.getVehicleType()); 84 params.put("vehicleProperty", vehicleListQuery.getVehicleProperty()); 85 params.put("vehicleOverallLength", vehicleListQuery.getVehicleOverallLength()); 86 params.put("organization", vehicleListQuery.getOrganization()); 87 params.put("partyId", loginSessionBean==null?"":loginSessionBean.getMerchantId().toString()); 88 String s = new ClientUtils().doPostMethod("http://mt-magicCube-vip:8080/magicCubeService/dHome/vehicle/exportVehicleList", params, "UTF-8"); 89 List<ExportVehicleRelationDto> exportVehicleRelationDtos = JSONArray.parseArray(s,ExportVehicleRelationDto.class); 90 return exportVehicleRelationDtos; 91 }