RestTemplate 微信接口 text/plain HttpMessageConverter

一、背景介绍

使用 Spring Boot 写项目,需要用到微信接口获取用户信息。

在 Jessey 和 Spring RestTemplate 两个 Rest 客户端中,想到尽量不引入更多的东西,然后就选择了 Spring RestTemplate 作为 网络请求的 Client,然后就被微信接口摆了一道,然后踩了一个 RestTemplate 的坑。

二、第一个坑:被微信摆了一道

报错信息是:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.solar.app.model.weixin.WxBaseUserInfo] and content type [text/plain]

之所以被微信摆了一道,是因为微信接口文档虽说返回的是 Json 数据,但是同时返回的 Header 里面的 Content-Type 值确是 text/plain 的!!

最终结果就是导致 RestTemplate 把数据从 HttpResponse 转换成 Object 的时候,找不到合适的 HttpMessageConverter 来转换!

我使用 RestTemplate 时配置 Bean 时使用默认的构造函数:

@Bean
RestTemplate restTemplate(){
    return new RestTemplate();
}

继续看 RestTemplate() 默认构造函数都干了啥:

/**
 * Create a new instance of the {@link RestTemplate} using default settings.
 * Default {@link HttpMessageConverter}s are initialized.
 */
public RestTemplate() {
    this.messageConverters.add(new ByteArrayHttpMessageConverter());
    this.messageConverters.add(new StringHttpMessageConverter());
    this.messageConverters.add(new ResourceHttpMessageConverter());
    this.messageConverters.add(new SourceHttpMessageConverter());
    this.messageConverters.add(new AllEncompassingFormHttpMessageConverter());

    if (romePresent) {
        this.messageConverters.add(new AtomFeedHttpMessageConverter());
        this.messageConverters.add(new RssChannelHttpMessageConverter());
    }

    if (jackson2XmlPresent) {
        this.messageConverters.add(new MappingJackson2XmlHttpMessageConverter());
    }
    else if (jaxb2Present) {
        this.messageConverters.add(new Jaxb2RootElementHttpMessageConverter());
    }

    if (jackson2Present) {
        this.messageConverters.add(new MappingJackson2HttpMessageConverter());// tag1
    }
    else if (gsonPresent) {
        this.messageConverters.add(new GsonHttpMessageConverter());
    }
}

可以看到,RestTemplate() 默认构造函数设置了一系列 HttpMessageConverter。

我的项目里引入了 com.fasterxml.jackson,所以 RestTemplate() 会构造一个 MappingJackson2HttpMessageConverter 加到它的 messageConverters 中,即上面的代码:【tag1】

继续看 MappingJackson2HttpMessageConverter() 默认构造函数:

/**
 * Construct a new {@link MappingJackson2HttpMessageConverter} using default configuration
 * provided by {@link Jackson2ObjectMapperBuilder}.
 */
public MappingJackson2HttpMessageConverter() {
    this(Jackson2ObjectMapperBuilder.json().build());
}

/**
 * Construct a new {@link MappingJackson2HttpMessageConverter} with a custom {@link ObjectMapper}.
 * You can use {@link Jackson2ObjectMapperBuilder} to build it easily.
 * @see Jackson2ObjectMapperBuilder#json()
 */
public MappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {
    super(objectMapper, MediaType.APPLICATION_JSON, new MediaType("application", "*+json"));
}

可以看到,默认构造的 MappingJackson2HttpMessageConverter 中的 supportedMediaTypes 只支持:application/json 的 MediaType。

再看 RestTemplate 请求的流程,会执行到这里:

/**
 * Execute the given method on the provided URI.
 * 

The {@link ClientHttpRequest} is processed using the {@link RequestCallback}; * the response with the {@link ResponseExtractor}. * @param url the fully-expanded URL to connect to * @param method the HTTP method to execute (GET, POST, etc.) * @param requestCallback object that prepares the request (can be {@code null}) * @param responseExtractor object that extracts the return value from the response (can be {@code null}) * @return an arbitrary object, as returned by the {@link ResponseExtractor} */ protected T doExecute(URI url, HttpMethod method, RequestCallback requestCallback, ResponseExtractor responseExtractor) throws RestClientException { Assert.notNull(url, "'url' must not be null"); Assert.notNull(method, "'method' must not be null"); ClientHttpResponse response = null; try { ClientHttpRequest request = createRequest(url, method); if (requestCallback != null) { requestCallback.doWithRequest(request); } response = request.execute(); handleResponse(url, method, response); if (responseExtractor != null) { return responseExtractor.extractData(response);// tag2 } else { return null; } } catch (IOException ex) { String resource = url.toString(); String query = url.getRawQuery(); resource = (query != null ? resource.substring(0, resource.indexOf(query) - 1) : resource); throw new ResourceAccessException("I/O error on " + method.name() + " request for \"" + resource + "\": " + ex.getMessage(), ex); } finally { if (response != null) { response.close(); } } }

从 HttpResponse 中获取数据实际是执行 【tag2】。这个操作由 HttpMessageConverterExtractor 类来完成:

@Override
@SuppressWarnings({"unchecked", "rawtypes", "resource"})
public T extractData(ClientHttpResponse response) throws IOException {
    MessageBodyClientHttpResponseWrapper responseWrapper = new MessageBodyClientHttpResponseWrapper(response);
    if (!responseWrapper.hasMessageBody() || responseWrapper.hasEmptyMessageBody()) {
        return null;
    }
    MediaType contentType = getContentType(responseWrapper);// tag3, 微信返回的是 text/plain

    for (HttpMessageConverter messageConverter : this.messageConverters) {
        if (messageConverter instanceof GenericHttpMessageConverter) {
            GenericHttpMessageConverter genericMessageConverter = (GenericHttpMessageConverter) messageConverter;
            if (genericMessageConverter.canRead(this.responseType, null, contentType)) {// tag4
                if (logger.isDebugEnabled()) {
                    logger.debug("Reading [" + this.responseType + "] as \"" +
                            contentType + "\" using [" + messageConverter + "]");
                }
                return (T) genericMessageConverter.read(this.responseType, null, responseWrapper);
            }
        }
        if (this.responseClass != null) {
            if (messageConverter.canRead(this.responseClass, contentType)) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Reading [" + this.responseClass.getName() + "] as \"" +
                            contentType + "\" using [" + messageConverter + "]");
                }
                return (T) messageConverter.read((Class) this.responseClass, responseWrapper);
            }
        }
    }

    throw new RestClientException("Could not extract response: no suitable HttpMessageConverter found " +
            "for response type [" + this.responseType + "] and content type [" + contentType + "]");
}

【tag4】处的代码用于判断 MappingJackson2HttpMessageConverter 是否支持 【tag3】 类型的 MediaType。

AbstractJackson2HttpMessageConverter:

@Override
public boolean canRead(Type type, Class contextClass, MediaType mediaType) {
    if (!canRead(mediaType)) {// tag5
        return false;
    }
    JavaType javaType = getJavaType(type, contextClass);
    if (!logger.isWarnEnabled()) {
        return this.objectMapper.canDeserialize(javaType);
    }
    AtomicReference causeRef = new AtomicReference();
    if (this.objectMapper.canDeserialize(javaType, causeRef)) {
        return true;
    }
    logWarningIfNecessary(javaType, causeRef.get());
    return false;
}

AbstractHttpMessageConverter:

/**
 * Returns {@code true} if any of the {@linkplain #setSupportedMediaTypes(List)
 * supported} media types {@link MediaType#includes(MediaType) include} the
 * given media type.
 * @param mediaType the media type to read, can be {@code null} if not specified.
 * Typically the value of a {@code Content-Type} header.
 * @return {@code true} if the supported media types include the media type,
 * or if the media type is {@code null}
 */
protected boolean canRead(MediaType mediaType) {
    if (mediaType == null) {
        return true;
    }
    for (MediaType supportedMediaType : getSupportedMediaTypes()) {
        if (supportedMediaType.includes(mediaType)) {
            return true;
        }
    }
    return false;
}

一路追踪下来,可以确定,只要让 MappingJackson2HttpMessageConverter 能处理头部 Content-Type 为 text/plain 类型的 Json 返回值的话,我们就能让其帮我们把 Json 反序列化成我们要的对象。

我们继承 MappingJackson2HttpMessageConverter 并在构造过程中设置其支持的 MediaType 类型即可:

public class WxMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
    public WxMappingJackson2HttpMessageConverter(){
        List mediaTypes = new ArrayList<>();
        mediaTypes.add(MediaType.TEXT_PLAIN);
        setSupportedMediaTypes(mediaTypes);// tag6
    }
}

【tag6】的代码,会覆盖其默认的 MediaType 设置。

然后把这个 WxMappingJackson2HttpMessageConverter 追加到 RestTemplate 的 messageConverters 消息转换链中去:

@Bean
RestTemplate restTemplate(){
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getMessageConverters().add(new WxMappingJackson2HttpMessageConverter());
    return restTemplate;
}

我既不推荐把 WxMappingJackson2HttpMessageConverter 实例当作构造 RestTemplate 时的参数来构造 RestTemplate,也不推荐 使用新的 WxMappingJackson2HttpMessageConverter 替换 RestTemplate 默认构造中创建的 MappingJackson2HttpMessageConverter 实例,因为这两种方式都会导致 Content-Type 为 application/json 的 Json 响应没有转换器来反序列化,所以最佳的方式还是“追加”。

三、第二个坑:RestTemplate 的使用

其实也不算坑,主要是我太蠢。
一开始我是这样写的:

@Override
public WxBaseUserInfo getBaseUserInfo(String access_token, String openid) {
    String url = "https://api.weixin.qq.com/sns/userinfo";

    Map params = new HashMap<>();
    params.put("access_token", access_token);
    params.put("openid", openid);
    params.put("lang", "zh_CN");

    WxBaseUserInfo result = null;
    try{
        result = restTemplate.getForObject(url, WxBaseUserInfo.class, params);
    }catch (RestClientException e){
        LOGGER.error("getBaseUserInfo", e);
    }
    return result;
}

但是,微信竟然提示我缺失 access_token !后来看 官方示例:REST in Spring 3: RestTemplate 才发现我用错了!正确用法是这样:

@Override
public WxBaseUserInfo getBaseUserInfo(String access_token, String openid) {
    String url = "https://api.weixin.qq.com/sns/userinfo?" +
            "access_token={access_token}&openid={openid}&lang={lang}";// tag7

    Map params = new HashMap<>();
    params.put("access_token", access_token);
    params.put("openid", openid);
    params.put("lang", "zh_CN");

    WxBaseUserInfo result = null;
    try{
        result = restTemplate.getForObject(url, WxBaseUserInfo.class, params);
    }catch (RestClientException e){
        LOGGER.error("getBaseUserInfo", e);
    }
    return result;
}

注意以上【tag7】处占位符的用法!

然后,还是有问题:如果因为 access_token 或 openid 的不合法,微信接口会返回一下格式的数据:

{ 
    "errcode":40003,"errmsg":"invalid openid"
}

经测试,当微信接口返回以上格式的错误信息 json 后,restTemplate.getForObject() 返回的仍然是一个我们想要的 WxBaseUserInfo 对象,但是该对象的任何字段都为 null!

经查,微信接口所有的错误时的 json 信息格式都如以上格式。然后迫不得己用一种很挫的方式来做“接口异常”处理:

public class WxError {

    private Integer errcode;

    private String errmsg;

    // getter and setter...

    @Override
    public String toString() {
        return "WxError{" +
                "errcode=" + errcode +
                ", errmsg='" + errmsg + '\'' +
                '}';
    }

    //---------- functions

    public boolean valid(){
        return errcode == null || errcode == 0;
    }
}

定义一个公共的错误信息类作为父类,所有微信正常返回的数据对象继承该错误类。

public class WxBaseUserInfo extends WxError {

    private String openid;

    private String nickname;

    private Integer sex;

    private String province;

    private String city;

    private String country;

    private String headimgurl;

    private List privilege;// tag8

    private String unionid;

    // getter and setter...

    @Override
    public String toString() {
        return "WxBaseUserInfo{" +
                "openid='" + openid + '\'' +
                ", nickname='" + nickname + '\'' +
                ", sex=" + sex +
                ", province='" + province + '\'' +
                ", city='" + city + '\'' +
                ", country='" + country + '\'' +
                ", headimgurl='" + headimgurl + '\'' +
                ", privilege='" + privilege + '\'' +
                ", unionid='" + unionid + '\'' +
                '}' + "  " + super.toString();
    }
}

注意以上的【tag8】处,privilege 类型是 List! 如果类写成 String 就会导致 Json 转换失败!

最终获取用户信息的方法变成了这样子:

@Override
public WxBaseUserInfo getBaseUserInfo(String access_token, String openid) {
    String url = "https://api.weixin.qq.com/sns/userinfo?" +
            "access_token={access_token}&openid={openid}&lang={lang}";

    Map params = new HashMap<>();
    params.put("access_token", access_token);
    params.put("openid", openid);
    params.put("lang", "zh_CN");

    WxBaseUserInfo result = null;
    try{
        result = restTemplate.getForObject(url, WxBaseUserInfo.class, params);
        if(null == result || !result.valid()){// tag9
            LOGGER.error("getBaseUserInfo invalid: " + result);
            result = null;
        }
    }catch (RestClientException e){
        LOGGER.error("getBaseUserInfo", e);
    }
    return result;
}

我这里的处理的当微信接口未能返回预期的数据时,此方法返回 null。换成 Java8 的 Optional 来处理应该会更好。大家按需处理吧。

四、总结

就这么一个简单的过程,我竟然踩了这么多坑,真是蠢。不过对也些东西的认识也加深了。如果您有更优雅的方式,请留言或者贴个链接呀,谢谢 :)

五、参考

  • REST in Spring 3: RestTemplate

你可能感兴趣的:(SpringMVC)