首先官方开发文档参考:https://developers.weixin.qq.com/miniprogram/dev/api-backend/open-api/qr-code/wxacode.createQRCode.html

生成的二维码类型有两种:

1:永久有效,但有数量限制  

POST https://api.weixin.qq.com/cgi-bin/wxaapp/createwxaqrcode?access_token=ACCESS_TOKEN
POST https://api.weixin.qq.com/wxa/getwxacode?access_token=ACCESS_TOKEN

 

2:永久有效,但无数量限制

POST https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=ACCESS_TOKEN

我这边用的是第二个,首先需要获得ACCESS_TOKEN

public String token(String appid,String secret) throws IOException {id = 2L;CloseableHttpClient httpClient = HttpClients.createDefault();String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid="+ appid +"&secret="+secret;HttpPost post = new HttpPost(url);CloseableHttpResponse response =httpClient.execute(post);String string = EntityUtils.toString(response.getEntity());System.out.println(string);response.close();httpClient.close();return string;
}

 

第二步就是调用接口了

public String QR(String access_token) throws IOException {   int count = 0;//双引号个数
//字符串处理,截取这边不做json转换,当然你可以拿去自己改成自己想用的for (int i = 0; i < string.length(); i++) {String s = string.substring(i, i + 1);if ("\"".equals(s)) {count++;}}List<String> list = new ArrayList<String>();for (int i = 0; i < count; i = i + 2) {string = string.substring(string.indexOf("\"") + 1, string.length());String strTemp = string.substring(0, string.indexOf("\""));string = string.substring(strTemp.length() + 1);list.add(strTemp);}String access_token = list.get(1);RestTemplate rest = new RestTemplate();InputStream inputStream = null;OutputStream outputStream = null;Long sceneStr = null;//传入的参数try {String url1 = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token="+ access_token;Map<String,Object> param = new HashMap<>();param.put("page", "pages/unit/unit");跳转的页面param.put("scene", sceneStr);param.put("width", 430);param.put("auto_color", true);param.put("is_hyaline", true);MultiValueMap<String, String> headers = new LinkedMultiValueMap<>();HttpEntity requestEntity = new HttpEntity(param, headers);ResponseEntity<byte[]> entity = rest.exchange(url1, HttpMethod.POST, requestEntity, byte[].class, new    Object[0]);byte[] result = entity.getBody();inputStream = new ByteArrayInputStream(result);UUID uuid = UUID.randomUUID();String filePathName = fileProperties.QRFilePath + uuid + ".png";//文件存储路径File file = new File(filePathName);if (!file.exists()){file.createNewFile();}outputStream = new FileOutputStream(file);int len = 0;byte[] buf = new byte[1024];while ((len = inputStream.read(buf, 0, 1024)) != -1) {outputStream.write(buf, 0, len);}outputStream.flush();} catch (Exception e) {e.getMessage();} finally {if(inputStream != null){try {inputStream.close();} catch (IOException e) {e.printStackTrace();}}if(outputStream != null){try {outputStream.close();} catch (IOException e) {e.printStackTrace();}}}return filePathName;
}

生成微信小程序指定页面二维码-编程知识网