手动方式
package com.imooc.controller; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; @RestController @RequestMapping("/weixin") @Slf4j public class WeixinController { @GetMapping("/auth") public void auth(@RequestParam("code") String code) { log.info("进入auth方法。。。"); log.info("code={}", code); String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxd898fcb01713c658&secret=29d8a650db31472aa87800e3b0d739f2&code=" + code + "&grant_type=authorization_code"; RestTemplate restTemplate = new RestTemplate(); String response = restTemplate.getForObject(url, String.class); log.info("response={}", response); } }
第三方SDK
<dependency> <groupId>com.github.binarywang</groupId> <artifactId>weixin-java-mp</artifactId> <version>2.7.0</version> </dependency>
wechat: # 公众账号, 授权 mpAppId: wxd898fcb01713c658 mpAppSecret: # 开放平台, 卖家扫码登录用 openAppId: wx6ad144e54af67d87 openAppSecret: 91a2ff6d38a2bbccfb7e9xxxxxx # 支付/商户号 mchId: 1483469312 mchKey: 06C56A89949D617xxxxxxxxxxx # 发起支付不需要证书, 退款需要 keyPath: notifyUrl: http://sell.natapp4.cc/sell/pay/notify templateId: orderStatus: e-Cqq67QxD6YNI41iRiqawEYdFavW_7pc7LyEMb-yeQ projectUrl: wechatMpAuthorize: http://lsx888.natapp1.cc wechatOpenAuthorize: http://lsx888.natapp1.cc sell: http://lsx888.natapp1.cc
package com.imooc.config; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Data @ConfigurationProperties(prefix = "project-url") @Component public class ProjectUrlConfig { /** * 微信公众平台授权url */ public String wechatMpAuthorize; /** * 微信开放平台授权url */ public String wechatOpenAuthorize; /** * 点餐系统 */ public String sell; }