Spring RestTemplate

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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容