使用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");
        }
    }

运行结果如下:

 

posted @ 2020-09-09 11:57  Shapley  阅读(1197)  评论(0编辑  收藏  举报