当前位置:网站首页>Get the third-party interface

Get the third-party interface

2022-06-11 05:22:00 Bug repair robot

Get the third-party interface

1.HttpUtils Tool class


import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.*;



public class HttpUtils {

    public static String doPost(String url, Map<String,String> map, String charset){
        CloseableHttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try{
            httpClient = HttpClients.createDefault();
            httpPost = new HttpPost(url);
            // Set parameters 
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator iterator = map.entrySet().iterator();
            while(iterator.hasNext()){
                Map.Entry<String,String> elem = (Map.Entry<String, String>) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
            }
            if(list.size() > 0){
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
                httpPost.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);
                }
            }
        }catch(Exception ex){
            ex.printStackTrace();
        }
        return result;
    }
}
2. Access interface 
   
@Api(value = "/", tags = " Bill related interface ")
@Controller
@RequestMapping("/merchant")
public class MerchantController {
    private static final Logger logger = LoggerFactory.getLogger(MerchantController.class);

   @Value("${merchantUrl}")
    private String merchantUrl;
   @RequestMapping(value = "getDetail", method = RequestMethod.GET)
    @ApiOperation(value = " For more details ")
    @ResponseBody
    public RtnInfo getDetail(@ApiParam(" bill id") @RequestParam(value = "id") String id) {
        logger.info("getDetail ============================>begin");
        logger.info("getDetail param: id=" + id);
        RtnInfo rtnInfo = new RtnInfo();
        Map<String, String> params = new HashMap<String, String>();
        params.put("id", id);
        String returnObj = HttpUtils.doPost(merchantUrl + "/logistics/free/getDetail", params, "utf-8");
        if (StringUtils.isBlank(returnObj)) {
            rtnInfo.setSystemCode(SystemCode.ERROR_5);
            rtnInfo.setMsg(" Interface returns no data !");
            return rtnInfo;
        }
        RtnInfo returnData = JSONObject.parseObject(returnObj, RtnInfo.class);
        if (returnData.getCode().equals("0")) {
            rtnInfo.setData(returnData.getData());
            rtnInfo.setSystemCode(SystemCode.SUCCESS);
        } else {
            rtnInfo.setData(null);
            rtnInfo.setSystemCode(SystemCode.ERROR_5);
            rtnInfo.setMsg(returnData.getMsg());
        }

        logger.info("returnResult:" + rtnInfo.toString());
        logger.info("getDetail ============================>end");
        return rtnInfo;
    }
	}

原网站

版权声明
本文为[Bug repair robot]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020540294227.html