Java微信公众号消息推送

一、在项目的配置文件中配置公众号信息

# 微信公众号配置
wx:
  appid: **********
  secret: **************
  token: *********************
  aeskey: ****************************

在这里插入图片描述
二、创建公众号类、达到将数据注入的目的

@Component
@ConfigurationProperties(prefix = "wx")
public class WxMpProperties {

    /**
     * 公众号appId
     */
    private String appId;

    /**
     * 公众号appSecret
     */
    private String secret;

    /**
     * 公众号token
     */
    private String token;

    /**
     * 公众号aesKey
     */
    private String aesKey;

    public String getAppId() {
        return appId;
    }

    public void setAppId(String appId) {
        this.appId = appId;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getAesKey() {
        return aesKey;
    }

    public void setAesKey(String aesKey) {
        this.aesKey = aesKey;
    }
}

三、创建配置类将公众号所需对象自动注入

@Configuration
public class WxConfig {
    private final WxMpProperties wxMpProperties;

    /**
     * 构造注入
     *
     * @param wxMpProperties
     */
    WxConfig(WxMpProperties wxMpProperties) {
        this.wxMpProperties = wxMpProperties;
    }

    /**
     * 微信客户端配置存储
     *
     * @return
     */
    @Bean
    public WxMpConfigStorage wxMpConfigStorage() {
        WxMpDefaultConfigImpl configStorage = new WxMpDefaultConfigImpl();
        // 公众号appId
        configStorage.setAppId(wxMpProperties.getAppId());
        // 公众号appSecret
        configStorage.setSecret(wxMpProperties.getSecret());
        // 公众号Token
        configStorage.setToken(wxMpProperties.getToken());
        // 公众号EncodingAESKey
        configStorage.setAesKey(wxMpProperties.getAesKey());
        return configStorage;
    }

    /**
     * 声明实例
     *
     * @return
     */
    @Bean
    public WxMpService wxMpService() {
        WxMpService wxMpService = new WxMpServiceImpl();
        wxMpService.setWxMpConfigStorage(wxMpConfigStorage());
        return wxMpService;
    }
}

三、实现发送信息的功能

@Component
public class WxMsgPush {
    /**
     * 微信公众号API的Service
     */
    private final WxMpService wxMpService;

    Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 构造注入
     */
    WxMsgPush(WxMpService wxMpService) {
        this.wxMpService = wxMpService;
    }


    /**
     * 发送微信模板信息
     *
     * @param openId 接受者openId
     * @return 是否推送成功
     */
    public Boolean SendWxMsg(String openId) {
        // 发送模板消息接口
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                // 接收者openid
                .toUser(openId)
                // 模板id
                .templateId("********************************")
                // 模板跳转链接(自定义)
                .url("http://www.baidu.com")
                .build();
        // 添加模板数据
        templateMessage.addData(new WxMpTemplateData("first", "*****", "#FF00FF"))
                .addData(new WxMpTemplateData("keyword1", "2021-01-28至2021-02-07", "#A9A9A9"))
                .addData(new WxMpTemplateData("keyword2", "***", "#FF00FF"))
                .addData(new WxMpTemplateData("keyword3","*****"))
                .addData(new WxMpTemplateData("remark", "这还是个测试", "#000000"));
        String msgId = null;
        try {
            // 发送模板消息
            msgId = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        logger.warn("·==++--·推送微信模板信息:{}·--++==·", msgId != null ? "成功" : "失败");
        return msgId != null;
    }
}

模板id:
在这里插入图片描述
没有可以先添加:
在这里插入图片描述
四、创建Controller层

@RestController
@RequestMapping(value = "/api/wxaccount",method = RequestMethod.POST)
public class SendWxAccountApi {
    /**
     * 微信消息推送
     */
    private WxMsgPush wxMsgPush;

    /**
     * 构造注入
     */
    protected SendWxAccountApi(WxMsgPush wxMsgPush) {
        this.wxMsgPush = wxMsgPush;
    }

    /**
     * 发送微信模板消息
     */
    @ApiOperation("发送微信模板消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "openId", value = "接受者openId", dataType = "String", paramType = "query")
    })
    @PostMapping("sendWxInfo")
    public void sendWxInfo(String openId) {
        // 执行发送
        Boolean aBoolean = wxMsgPush.SendWxMsg(openId);
        System.out.println(aBoolean);
    }

}


版权声明:本文为weixin_52277015原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。