接口提供方
get请求
@RequestMapping(value = "/updateUserHttp", method = RequestMethod.GET)
@ResponseBody
public String updateUserHttp() throws Exception {
ReturnInfoVo<Object> infoVo = this.returnInfoVoParser(updateUserHttpServiceImpl.updateUser());
String returnInfoVo = JSON.toJSONString(infoVo);
return returnInfoVo;
}
post请求
@RequestMapping("/updateHttpUser")
@ResponseBody
public String updateUserHttp(String inputInfo) throws Exception {
UserVo userVo= JSON.parseObject(inputInfo, UserVo.class);
ReturnInfoVo<Object> infoVo = this.returnInfoVoParser(userHttpServiceImpl.updatesysuser(userVo));
String returnInfoVo = JSON.toJSONString(infoVo);
return returnInfoVo;
}
接口调用方
public void testHttp() throws Exception{
Map<String, Object> requestProperty=new HashMap<String, Object>();
String postEntity = "";
UserVo userVo = new UserVo();
userVo.setUserId("001");
userVo.setUserName("kemi");
String jsonString = JSON.toJSONString(userVo);
requestProperty.put("inputInfo", jsonString);
String url="http://127.0.0.1:8088/userHttp/updateUserHttp.html";
postEntity = HttpClientUtil.postEntity(url, requestProperty);
if(StringUtils.isNotBlank(postEntity)){
JSONObject text= JSON.parseObject(postEntity);
System.out.println(text.toJSONString());
}
}
HttpClientUtil.java 工具类
public static String get(String url) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url);
HttpResponse response = httpclient.execute(httpget);
HttpEntity entity = response.getEntity();
String html = EntityUtils.toString(entity,"UTF-8");
httpclient.close();
return html;
}
public static String post(String url,String params) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
StringEntity entity = new StringEntity(params,"utf-8");
entity.setContentType("application/x-www-form-urlencoded");
entity.setContentEncoding("UTF-8");
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity1 = response.getEntity();
String html = EntityUtils.toString(entity1,"UTF-8");
httpclient.close();
return html;
}
public static String postEntity(String url, Map<String, Object> params) throws Exception {
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create();
Set<String> keySet = params.keySet();
for(String key : keySet) {
Object obj=params.get(key);
if(obj instanceof String){
reqEntity.addPart(key,new StringBody((String)obj, ContentType.TEXT_PLAIN));
}else if(obj instanceof File){
FileBody fileBody = new FileBody((File)obj);
reqEntity.addPart(key, fileBody);
}
}
httppost.setEntity(reqEntity.build());
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity1 = response.getEntity();
String html = EntityUtils.toString(entity1,"UTF-8");
httpclient.close();
return html;
}