• 远程调用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());
        }
    }
  • 相关阅读:
    python 类继承演示范例的代码
    C#监控指定目录的文件变化的代码
    如何开启红米手机4X的ROOT超级权限
    C# 发送电子邮件源码片段
    android中使用afinal一行源码显示网络图片
    (详细)华为畅享7 SLA-AL00的usb调试模式在哪里打开的流程
    C#自定义FTP访问类的代码
    C#的String.Split 分割字符串用法详解的代码
    红米Note 5A完美卡刷开发版获得ROOT超级权限的方法
    C# 类如何声明索引器以提供对类的类似数组的访问的代码
  • 原文地址:https://www.cnblogs.com/xianz666/p/13588995.html
Copyright © 2020-2023  润新知