错误代码:
String requestUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + rongWxSet.getWxAppid() + "&secret=" + rongWxSet.getWxSecret() + "&code=" + wx_code + "&grant_type=authorization_code";
WxBackResult wxBackResult = restTemplate.getForObject(requestUrl,WxBackResult .class);
报错:unacceptable content-type: text/plain,
主要意思就是访问微信接口后返回的结果不能直接映射到自定义类型中。
解决方法:
String requestUrl="https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + rongWxSet.getWxAppid() + "&secret=" + rongWxSet.getWxSecret() + "&code=" + wx_code + "&grant_type=authorization_code";
String re = restTemplate.getForObject(requestUrl,String.class);
WxBackResult wxBackResult = null;
try {
wxBackResult = objectMapper.readValue(re,WxBackResult.class);
} catch (IOException e) {
e.printStackTrace();
}
其实微信接口返回结果就是标准的json格式的字符串,所以我们先用字符串接受,然后转化成我们的自定义对象。
自定义对象:WxBackResult
根据微信的返回结果自定义的结果对象
@Data
public class WxBackResult {
@ApiModelProperty(value = "微信返回的token")
private String access_token;
@ApiModelProperty(value = "调用微信接口返回的openId")
private String openid;
private Integer errcode;
private String errmsg;
}