自定义微信开发 spring-boot-starter
程序员文章站
2022-06-23 11:12:51
自定义微信开发 spring-boot-starter1.新建项目推荐使用 xx-spring-boot-starter2.引入依赖 org.springframework.boot spring-boot-starter-parent 2.1.3.R...
自定义微信开发 spring-boot-starter
1.新建项目
推荐使用 xx-spring-boot-starter
2.引入依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
</parent>
<dependencies>
<!--封装Starter核心依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>
</dependency>
<!--非必需,配置文件有代码提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
<!--非必需,存储微信名包含emjoy转为字符-->
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>java-emoji-converter</artifactId>
<version>0.1.1</version>
</dependency>
<!--非必需 https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
</dependencies>
3.编写代码
3.1写配置类 例如:
/**
* @Description: 配置类
* @Author: tyc
* @CreateDate: 2020/12/29 15:05
*/
@ConfigurationProperties(prefix = "wx")
public class WxProperties {
//公众号服务器验证token
private String token;
//微信公众号的ID
private String public_appid;
//微信公众号的SECRT
private String public_secret;
//微信公众号模板消息ID
private String public_templateid;
//小程序的ID
private String mini_appid;
//小程序的appsecret
private String mini_secret;
//微信小程序案件处理完成通知模板id
private String mini_templateid;
}
3.2 提供外部使用的接口 例如:
/**
* @Description: java类作用描述
* @Author: tyc
* @CreateDate: 2020/12/29 16:54
*/
public interface WxService {
//微信小程序相关
String getAppAccessToken();
JSONObject getAppLoginInfo(String code);
JSONObject getAppUserPhone(String encryptedData,String session_key, String iv);
Boolean sendTemplateMessageToApp(String access_token,String message);
//微信公众号相关方法
//配置服务器
Boolean authenticate(String signature,String timestamp,String nonce);
String getPubAccessYoken();
Map<String, String> xmlToMap(HttpServletRequest request);
String getPubH5Openid(String code);
boolean sendTemplateMessage(String access_token,String message);
}
3.3 编写自动配置类 例如:
/**
* @Description: 自动配置
* @Author: tyc
* @CreateDate: 2020/12/29 16:50
*/
@Configuration
@ConditionalOnMissingBean(WxService.class)
@EnableConfigurationProperties({WxProperties.class})
public class WxAutoConfigure {
@Autowired
private WxProperties wxProperties;
@Bean
public WxService wxService(){
WxService wxService = new WxServiceImpl();
return wxService;
}
}
4 最终配置
在resources包下新建文件夹 META-INF 在META-INF下新建名为spring.factories的文件,写配置 例如:
目的:获取所有自动配置类的全类名
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.tyc.wx.conifg.WxAutoConfigure
5.打包测试
打包完成后,新建项目引入starter依赖,写配置,单元测试:
wx:
mini-appid: 你的小程序id
mini-secret: 你的小程序密匙
@GetMapping("/test")
public String getToken(){
String accessToken = wxService.getAppAccessToken();
log.info(accessToken);
return accessToken;
}
2020-12-30 11:08:26.102 INFO 13904 --- [nio-8009-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2020-12-30 11:08:26.111 INFO 13904 --- [nio-8009-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 9 ms
2020-12-30 11:08:27.114 INFO 13904 --- [nio-8009-exec-1] c.hanmo.file.controller.FileController : 40_3pQTP7T-7ZyhxyenQ57W92O3b_WTFDr8UHHEmtOpiSNrvFIz3XgjdfbNyE_fGIG-OUzc1uIFVmXtztxE2Wlx6vWtDdK1Q25e9n39VmauRcD2-XLnFOsfFCxT9S96zIFG7LFDsWDcj94QbQqlDZLgAGAYUG
本文地址:https://blog.csdn.net/weixin_44795440/article/details/111957444
上一篇: GUI编程之Swing中JFrame窗体