springboot添加feign client

本文介绍如何使用Spring Cloud OpenFeign简化对外部服务的调用,以微信API为例,展示了如何配置Feign Client,实现从远程服务获取openid和session_key等功能。

pom

 <!--feign 接口调用-->
 <dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-openfeign</artifactId>
	<version>2.2.0.RELEASE</version>
 </dependency>

添加扫描

// 项目启动扫描client包
@EnableFeignClients("com.xxx.framework.xxx.client")
@SpringBootApplication

client代码

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
/**
 * 微信client
 */
@FeignClient(name = "WeChat", url = "https://api.weixin.qq.com")
public interface WeChatClient {

    /**
     * 获取登录微信的openid和session_key等信息
     * @param appid
     * @param secret
     * @param jsCode
     * @param grantType
     * @return
     */
    @PostMapping("/sns/jscode2session")
    String getSessionKeyOrOpenId(
        @RequestParam("appid") String appid,
        @RequestParam("secret") String secret,
        @RequestParam("js_code") String jsCode,
        @RequestParam("grant_type") String grantType
    );
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值