在使用微信第三方SDK实现消息订阅时发现该错误
意思是无法在包外调用该方法.在使用依赖包时如果是2.7.0则不会出现该问题.但使用2.9.0以上的版本时则会出现该问题.
<dependency><groupId>com.github.binarywang</groupId><artifactId>weixin-java-mp</artifactId><version>2.7.0</version>
</dependency>
这是因为新版本采用了 lombok的@Builder 注解然后.官网应该是更推荐使用以builder的方法来构造对象,因此直接限制了该类构造器权限,无法在包外访问.
这里可以了解一下 lombok@Builder注解的使用
使用@Builder的优点是:
- 不需些太多的set方法来定义属性内容
- 写法更优雅
直接上代码
@Service
@Transactional
public class WeChatService {@Autowiredprivate WxMpService wxMpService;@Autowiredprivate WechatConfig wechatConfig;/*** 预约状态变更消息*/public void applyStatus(){WxMpTemplateMessage.MiniProgram miniProgram = new WxMpTemplateMessage.MiniProgram(wechatConfig.getAppId(), "/pages/home/index");List<WxMpTemplateData> data = Arrays.asList(new WxMpTemplateData("time16.DATA", "预约时间"),new WxMpTemplateData("thing34.DATA", "咨询地点"),new WxMpTemplateData("phone_number4.DATA", "18868812345"),new WxMpTemplateData("thing10.DATA", "备注"));WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder().toUser("openid").templateId(wechatConfig.getTemplateId().get("applyStatus")).miniProgram(miniProgram).data(data).build();try {String s = wxMpService.getTemplateMsgService().sendTemplateMsg(templateMessage);} catch (WxErrorException e) {e.printStackTrace();}}
}