RestClientException: Could not extract response. no suitable HttpMessageConverter found

后端 未结 11 2064
感情败类
感情败类 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:45

    In my case @Ilya Dyoshin's solution didn't work: The mediatype "*" was not allowed. I fix this error by adding a new converter to the restTemplate this way during initialization of the MockRestServiceServer:

      MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter = 
                          new MappingJackson2HttpMessageConverter();
      mappingJackson2HttpMessageConverter.setSupportedMediaTypes(
                                        Arrays.asList(
                                           MediaType.APPLICATION_JSON, 
                                           MediaType.APPLICATION_OCTET_STREAM));
      restTemplate.getMessageConverters().add(mappingJackson2HttpMessageConverter);
      mockServer = MockRestServiceServer.createServer(restTemplate);
    

    (Based on the solution proposed by Yashwant Chavan on the blog named technicalkeeda)

    JN Gerbaux

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

    If the above response by @Ilya Dyoshin didn't still retrieve, try to get the response into a String Object.

    (For my self thought the error got solved by the code snippet by Ilya, the response retrieved was a failure(error) from the server.)

    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.add(HttpHeaders.CONTENT_TYPE, "application/x-www-form-urlencoded");
    ResponseEntity<String> st = restTemplate.exchange(url, HttpMethod.POST, httpEntity, String.class); 
    

    And Cast to the ResponseObject DTO (Json)

    Gson g = new Gson();
    DTO dto = g.fromJson(st.getBody(), DTO.class); 
    
    0 讨论(0)
  • 2020-12-13 08:48

    Other possible solution : I tried to map the result of a restTemplate.getForObject with a private class instance (defined inside of my working class). It did not work, but if I define the object to public, inside its own file, it worked correctly.

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

    I was having a very similar problem, and it turned out to be quite simple; my client wasn't including a Jackson dependency, even though the code all compiled correctly, the auto-magic converters for JSON weren't being included. See this RestTemplate-related solution.

    In short, I added a Jackson dependency to my pom.xml and it just worked:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.5.1</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-13 08:51

    In my case it was caused by the absence of the jackson-core, jackson-annotations and jackson-databind jars from the runtime classpath. It did not complain with the usual ClassNothFoundException as one would expect but rather with the error mentioned in the original question.

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

    You need to create your own converter and implement it before making a GET request.

    RestTemplate  restTemplate = new RestTemplate();
    
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();        
    
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));         
    messageConverters.add(converter);  
    restTemplate.setMessageConverters(messageConverters);    
    
    0 讨论(0)
提交回复
热议问题