get请求
# 地址
String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={id}&secret={secret}";
# 发送的get数据
Map<String,Object> map = new HashMap<>(3);
map.put("id", APPID);
map.put("secret", SECRET);
# 发请求获取内容
ResponseEntity<String> entity = restTemplate.getForEntity(url, String.class,map);
ObjectMapper objectMapper = new ObjectMapper();
try {
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
return objectMapper.readValue(entity.getBody(), WxToken.class);
} catch (IOException e) {
return null;
}
post请求
String data;
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
try {
data = objectMapper.writeValueAsString(template);
}catch (Exception e){
return false;
}
String url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={token}";
Map<String,Object> map = new HashMap<>(1);
map.put("token", settingService.getSettingValue(2));
HttpHeaders headers = new HttpHeaders();
headers.add("Accept", MediaType.APPLICATION_JSON.toString());
HttpEntity<String> formEntity = new HttpEntity<>(data, headers);
ResponseEntity<String> entity = restTemplate.postForEntity(url,formEntity,String.class,map);
System.out.println(entity.getBody());
try {
objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
TemplateSend templateSend = objectMapper.readValue(entity.getBody(), TemplateSend.class);
} catch (IOException e) {
return null;
}