RestClientException: Could not extract response. no suitable HttpMessageConverter found

后端 未结 11 2065
感情败类
感情败类 2020-12-13 08:17

Using the curl command:

curl -u 591bf65f50057469f10b5fd9:0cf17f9b03d056ds0e11e48497e506a2 https://backend.tdk.com/api/devicetypes/59147fd79e93s12e61499ffe/me         


        
相关标签:
11条回答
  • 2020-12-13 08:55

    While the accepted answer solved the OP's original problem, I suspect most people finding this question through a Google search are likely (statistically speaking) to be having an entirely different problem which just happens to throw the same no suitable HttpMessageConverter found exception.

    What happens under the covers is that MappingJackson2HttpMessageConverter swallows any exceptions that occur in its canRead() method, which is supposed to auto-detect whether the payload is suitable for json decoding. The exception is replaced by a simple boolean return that basically communicates sorry, I don't know how to decode this message to the higher level APIs (RestClient). Only after all other converters' canRead() methods return false, the no suitable HttpMessageConverter found exception is thrown by the higher-level API, totally obscuring the true problem.

    For people who have not found the root cause (like you and me, but not the OP), the way to troubleshoot this problem is to place a debugger breakpoint on onMappingJackson2HttpMessageConverter.canRead(), then enable a general breakpoint on any exception, and hit Continue. The next exception is the true root cause.

    My specific error happened to be that one of the beans referenced an interface that was missing the proper deserialization annotations.

    0 讨论(0)
  • 2020-12-13 08:56

    Spring sets the default content-type to octet-stream when the response is missing that field. All you need to do is to add a message converter to fix this.

    0 讨论(0)
  • 2020-12-13 09:02

    Here is the approach I follow whenever I see this type of error:

    1. One way to debug this issue is by first taking whatever response is coming as String.class then applying Gson().fromJson(StringResp.body(), MyDTO.class). It will still fail most probably but this time it will throw the fields which are creating this error to happen in first place. Post the modification, we can use the previous approach as usual.

    ResponseEntity<String> respStr = restTemplate.exchange(URL,HttpMethod.GET, entity, String.class);
    Gson g = new Gson();

    The below step will throw error with the fields which is causing the issue

    MyDTO resp = g.fromJson(respStr.getBody(), MyDTO.class);
    

    I don't have the error message with me but it will point to the field which is problematic and the reason for it. Resolve those and try again with previous approach.

    0 讨论(0)
  • 2020-12-13 09:06

    I was trying to use Feign, while I encounter same issue, As I understood HTTP message converter will help but wanted to understand how to achieve this.

    @FeignClient(name = "mobilesearch", url = "${mobile.search.uri}" ,
            fallbackFactory = MobileSearchFallbackFactory.class,
            configuration = MobileSearchFeignConfig.class)
    public interface MobileSearchClient {
    
        @RequestMapping(method = RequestMethod.GET)
        List<MobileSearchResponse> getPhones();
    }
    

    You have to use Customer Configuration for the decoder, MobileSearchFeignConfig,

    public class MobileSearchFeignConfig {
    
        @Bean
        Logger.Level feignLoggerLevel() {
            return Logger.Level.FULL;
        }
    
    
        @Bean
        public Decoder feignDecoder() {
            return new ResponseEntityDecoder(new SpringDecoder(feignHttpMessageConverter()));
        }
    
        public ObjectFactory<HttpMessageConverters> feignHttpMessageConverter() {
            final HttpMessageConverters httpMessageConverters = new HttpMessageConverters(new MappingJackson2HttpMessageConverter());
            return new ObjectFactory<HttpMessageConverters>() {
                @Override
                public HttpMessageConverters getObject() throws BeansException {
                    return httpMessageConverters;
                }
            };
        }
    
        public class MappingJackson2HttpMessageConverter extends org.springframework.http.converter.json.MappingJackson2HttpMessageConverter {
            MappingJackson2HttpMessageConverter() {
                List<MediaType> mediaTypes = new ArrayList<>();
                mediaTypes.add(MediaType.valueOf(MediaType.TEXT_HTML_VALUE + ";charset=UTF-8"));
                setSupportedMediaTypes(mediaTypes);
            }
        }
    
    }
    
    0 讨论(0)
  • 2020-12-13 09:10

    The main problem here is content type [text/html;charset=iso-8859-1] received from the service, however the real content type should be application/json;charset=iso-8859-1

    In order to overcome this you can introduce custom message converter. and register it for all kind of responses (i.e. ignore the response content type header). Just like this

    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();        
    //Add the Jackson Message converter
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    
    // Note: here we are making this converter to process any kind of response, 
    // not only application/*json, which is the default behaviour
    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));        
    messageConverters.add(converter);  
    restTemplate.setMessageConverters(messageConverters); 
    
    0 讨论(0)
提交回复
热议问题