引入依赖:
<dependency>
<groupId>com.github.binarywang</groupId>
<artifactId>weixin-java-mp</artifactId>
<version>3.9.0</version>
</dependency>
代码:
try {
String url = null;
WxMpDefaultConfigImpl wxMpDefaultConfig = new WxMpDefaultConfigImpl();
wxMpDefaultConfig.setAppId("wxxxxxxxxxxxxx");
wxMpDefaultConfig.setSecret("aaaaaaaaaaaaaaaaaa");
wxMpDefaultConfig.setToken("");
wxMpDefaultConfig.setAesKey("");
WxMpService wxMpService = new WxMpServiceImpl();
wxMpService.setWxMpConfigStorage(wxMpDefaultConfig);
//换取临时二维码ticket,设置过期时间-最大不超过2592000(即30天)。
WxMpQrCodeTicket ticket = wxMpService.getQrcodeService().qrCodeCreateTmpTicket("login_"+new Date().getTime(),2592000);
//换取永久二维码ticket
//WxMpQrCodeTicket ticket = wxConfig.getWxMpService().getQrcodeService().qrCodeCreateLastTicket(sceneId);
//拿ticket换取二维码图片url地址
url = wxMpService.getQrcodeService().qrCodePictureUrl(ticket.getTicket());
System.out.println(url);
} catch (Exception e){
e.printStackTrace();
}