Http方式调用外部接口

接口提供方

get请求

@RequestMapping(value = "/updateUserHttp", method = RequestMethod.GET)
	@ResponseBody
	public String updateUserHttp() throws Exception {
		ReturnInfoVo<Object> infoVo = this.returnInfoVoParser(updateUserHttpServiceImpl.updateUser());
		// 将返回信息转成JSON串
		String returnInfoVo = JSON.toJSONString(infoVo);
		return returnInfoVo;
	}

post请求

@RequestMapping("/updateHttpUser")
	@ResponseBody
	public String updateUserHttp(String inputInfo) throws Exception {
		//用实体转换JSON数据
		UserVo userVo= JSON.parseObject(inputInfo, UserVo.class);
		ReturnInfoVo<Object> infoVo = this.returnInfoVoParser(userHttpServiceImpl.updatesysuser(userVo));
		//将返回信息转成JSON串
		String returnInfoVo = JSON.toJSONString(infoVo);
		return returnInfoVo;
	}

接口调用方

public void testHttp() throws Exception{
		//调用http接口
		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";
		//post请求
		postEntity = HttpClientUtil.postEntity(url, requestProperty);
		//get请求
		//postEntity= HttpClientUtil.get(url);
		if(StringUtils.isNotBlank(postEntity)){
		//返回的实体转换JSON数据
		JSONObject text= JSON.parseObject(postEntity);
			System.out.println(text.toJSONString());
		}
	}

HttpClientUtil.java 工具类

/**
	 * get
	 * @throws Exception
	 */
	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;
     }
	
	/**
	 * post
	 * @throws Exception
	 */
	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;
	}
	
/**
	 * post
	 * @throws Exception
	 */
	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;
	}
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值