zoukankan      html  css  js  c++  java
  • 远程调用get和post请求 将返回结果转换成实体类

    package org.springblade.desk.utils;
    
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    import org.springblade.core.tool.utils.Func;
    
    /**
     * create by Dell on 2020/6/17
     */
    public class HttpUtil {
        //get请求
        public static String doGet(String url,String authValue){
            String result = null;
            CloseableHttpClient httpClient = HttpClients.createDefault();
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            try {
                HttpGet httpGet = new HttpGet(url);
                httpGet.setHeader("Content-type", "application/json");
                if(!Func.isEmpty(authValue)){
                    httpGet.setHeader("Authorization","Bearer "+authValue);
                }
                result = httpClient.execute(httpGet, responseHandler);
            } catch (Exception e) {
                e.printStackTrace();
            }
            finally {
                try {
                    httpClient.close();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
            return result ;
        }
    
        // post请求参数为json格式
        public static String doJsonPost(String url, String json,String authValue) {
            String result = null;
            CloseableHttpClient httpClient = HttpClients.createDefault();
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            try {
                HttpPost httpPost = new HttpPost(url);
                StringEntity requestEntity = new StringEntity(json, "utf-8");
                requestEntity.setContentEncoding("UTF-8");
                httpPost.setHeader("Content-type", "application/json");
                if(!Func.isEmpty(authValue)){
                    httpPost.setHeader("Authorization","Bearer "+authValue);
                }
                httpPost.setEntity(requestEntity);
                result = httpClient.execute(httpPost, responseHandler);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            } finally {
                try {
                    httpClient.close();
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
            }
            return result;
        }
    }
    public class PartnerController extends BladeController implements CacheNames {
    
        /**
         * 初始化
         *
         */
        @GetMapping("/init")
        @ApiOperationSupport(order = 1)
        @ApiOperation(value = "初始化", notes = "传入参数")
        public R<PartnerInitData> init(String company, String cardType) {
            String url=String.format(SapConstant.PARTNER_INIT +"?Company={0}&CardType={1}",company,cardType);
            String res = HttpUtil.doGet(url,null);
            PartnerInitResult result = JSONObject.parseObject(res, PartnerInitResult.class);
            return R.data(result.getData());
        }
    
        /**
         * 详情
         */
        @GetMapping("/detail")
        @ApiOperationSupport(order = 2)
        @ApiOperation(value = "详情", notes = "传入参数")
        public R<PartnerDetailData> detail(String company, String cardCode) {
            String url = SapConstant.PARTNER_DETAIL +"?Company="+company+"&CardCode="+cardCode;
            String res = HttpUtil.doGet(url,SapUtil.getReadToken());
            PartnerDetailResult result = JSONObject.parseObject(res, PartnerDetailResult.class);
            return R.data(result.getData());
        }
    
        /**
         * 查询
         */
        @GetMapping("/query")
        @ApiOperationSupport(order = 3)
        @ApiOperation(value = "查询", notes = "传入参数")
        public R<List<HashMap<String,String>>> query(String company, String cardType, String partner) {
            String url = SapConstant.PARTNER_QUERY +"?Company="+company+"&CardType="+cardType+"&Partner="+partner;
            String res = HttpUtil.doGet(url,SapUtil.getReadToken());
            PartnerQueryResult result = JSONObject.parseObject(res, PartnerQueryResult.class);
            return R.data(result.getData());
        }
    
        /**
         * 特殊价格详情
         */
        @GetMapping("/specprices/detail")
        @ApiOperationSupport(order = 4)
        @ApiOperation(value = "特殊价格详情", notes = "传入参数")
        public R<List<HashMap<String,Object>>> specPricesDetail(String company, String cardCode) {
            String url = SapConstant.PARTNER_SPECPRICES_DETAIL +"?Company="+company+"&CardCode="+cardCode;
            String res = HttpUtil.doGet(url,SapUtil.getReadToken());
            SpecPricesDetailResult result = JSONObject.parseObject(res, SpecPricesDetailResult.class);
            return R.data(result.getData());
        }
    
        /**
         * 特殊价格查询
         */
        @GetMapping("/specprices/query")
        @ApiOperationSupport(order = 5)
        @ApiOperation(value = "特殊价格查询", notes = "传入参数")
        public R<List<HashMap<String,Object>>> specPricesQuery(String company, String cardCode, String itemCode) {
            String url = SapConstant.PARTNER_SPECPRICES_QUERY +"?Company="+company+"&CardCode="+cardCode+"&ItemCode="+itemCode;
            String res = HttpUtil.doGet(url,SapUtil.getReadToken());
            SpecPricesQueryResult result = JSONObject.parseObject(res, SpecPricesQueryResult.class);
            return R.data(result.getData());
        }
    }
  • 相关阅读:
    Ribbon 和 Eureka 积分
    zabbix 实现curl 显示器
    《算法入门经典大赛——培训指南》第二章考试
    今天你还抽象?
    Big Data Security Part One: Introducing PacketPig
    Big Data Analytics for Security(Big Data Analytics for Security Intelligence)
    CA
    通过Shell和Redis来实现集群业务中日志的实时收集分析
    用Maven编译Apache flume-ng 1.5.0源码及问题解决
    java8-concurrency-tutorial-thread-executor-examples
  • 原文地址:https://www.cnblogs.com/xianz666/p/13588995.html
Copyright © 2011-2022 走看看