• 使用FeignClient调用微信接口并返回openid简例


    在Java开发过程中可以使用各种http工具类调用微信接口,由于springCloud已经成为主流,其自带FeignClient类已经很优雅地实现了各种http调用方式,因此在springCloud中可以优先使用这个类调用微信接口。

    所需材料:

    1.实体定义:

    @Entity
    public class WeixinJsonObject {
        @Id
        private String openid;
        private String session_key;
        private String unionid;
        private String errcode;
        private String errmsg;
    
        public String getOpenid() {
            return openid;
        }
    
        public void setOpenid(String openid) {
            this.openid = openid;
        }
    
        public String getSession_key() {
            return session_key;
        }
    
        public void setSession_key(String session_key) {
            this.session_key = session_key;
        }
    
        public String getUnionid() {
            return unionid;
        }
    
        public void setUnionid(String unionid) {
            this.unionid = unionid;
        }
    
        public String getErrcode() {
            return errcode;
        }
    
        public void setErrcode(String errcode) {
            this.errcode = errcode;
        }
    
        public String getErrmsg() {
            return errmsg;
        }
    
        public void setErrmsg(String errmsg) {
            this.errmsg = errmsg;
        }
    }

    2.FeignClient类代码:

    @FeignClient(name = "${service.request.name}", configuration = FoodMobileServiceHystrix.class,url = "https://api.weixin.qq.com/")
    public interface FoodMobileService {
        @RequestMapping(value = "/sns/jscode2session", method = RequestMethod.GET)
        String getWeixinLogin(@RequestParam(name = "appid", required = true) String appid,
                                        @RequestParam(name = "secret", required = true) String secret,
                                        @RequestParam(name = "js_code", required = true) String js_code,
                                        @RequestParam(name = "grant_type", required = true) String grant_type);
    }
    FoodMobileServiceHystrix类代码:
    public class FoodMobileServiceHystrix  implements FoodMobileService {
        @Override
        public String getWeixinLogin(String appid, String secret, String js_code, String grant_type) {
            return null;
        }
    }

    3.接口调用代码:

    @ApiOperation(value = "获取微信ID", notes = "获取微信ID")
        @RequestMapping(value = "/getWeixinLogin", method = RequestMethod.GET, produces = "application/json; charset=UTF-8")
        public ResponseResult<Object> getWeixinLogin(@Param("js_code") String js_code) {
            try {
                String appid = "***********";
                String secret = "*******************";
                String grant_type = "authorization_code";
                String result = foodMobileService.getWeixinLogin(appid, secret, js_code, grant_type);
    
                JSONObject jsonString = JSONObject.parseObject(result);
                WeixinJsonObject weixinJsonObject = (WeixinJsonObject) JSONObject.toJavaObject(jsonString, WeixinJsonObject.class);
                return RestResultGenerator.genResult(weixinJsonObject, ConstantCode.RETURN_MESSAGE_SUCCESS, true, "200");
    
            } catch (Exception ex) {
                logger.error("方法名:getWeixinLogin错误:" + ex.getMessage());
                ex.printStackTrace();
                return RestResultGenerator.genResult(null, ex.getMessage(), false, "500");
            }
        }

    运行结果如下:

  • 相关阅读:
    VS2019正式版 密钥 Visual Studio 2019 破解 激活码 Key
    关于随机数的前世今生
    木兮的纪中集训感想
    浅谈欧洲算法——模拟退火
    你没听过的梅森旋转算法
    二分贪心杂题
    DP专项训练
    实验八 进程间通信
    实验七 信号
    实验六 进程基础
  • 原文地址:https://www.cnblogs.com/jizhong/p/13637694.html
Copyright © 2020-2023  润新知