美文网首页
Spring RestTemplate

Spring RestTemplate

作者: 公子只識黛玉 | 来源:发表于2018-06-28 15:28 被阅读0次

    public class HttpMessageConverterExtractorimplements ResponseExtractor {

    ...........................

    }

    Exception in thread "main" org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.lang.String] and content type [text/html;charset=utf-8]

    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:110)

    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:655)

    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:613)

    at org.springframework.web.client.RestTemplate.postForObject(RestTemplate.java:380)

    at HttpUtils.HttpUtils.sendGeneralRequest(HttpUtils.java:115)

    @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);

      for (HttpMessageConverter messageConverter :this.messageConverters) {

    if (messageConverterinstanceof GenericHttpMessageConverter) {

    GenericHttpMessageConverter genericMessageConverter =

    (GenericHttpMessageConverter) messageConverter;

            if (genericMessageConverter.canRead(this.responseType, null, contentType)) {

    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 +"]");

    }

    protected boolean canRead(MediaType mediaType) {

    if (mediaType ==null) {

    return true;

      }

    for (MediaType supportedMediaType : getSupportedMediaTypes()) {

    if (supportedMediaType.includes(mediaType)) {

    return true;

          }

    }

    return false;

    }

    protected boolean canWrite(MediaType mediaType) {

    if (mediaType ==null || MediaType.ALL.equals(mediaType)) {

    return true;

      }

    for (MediaType supportedMediaType : getSupportedMediaTypes()) {

    if (supportedMediaType.isCompatibleWith(mediaType)) {

    return true;

          }

    }

    return false;

    }

    public interface HttpMessageConverter {

    boolean canRead(Class var1, MediaType var2);

        boolean canWrite(Class var1, MediaType var2);

        List getSupportedMediaTypes();

        T read(Class var1, HttpInputMessage var2)throws IOException, HttpMessageNotReadableException;

        void write(T var1, MediaType var2, HttpOutputMessage var3)throws IOException, HttpMessageNotWritableException;

    }

    相关文章

      网友评论

          本文标题:Spring RestTemplate

          本文链接:https://www.haomeiwen.com/subject/wcwgyftx.html