no suitable HttpMessageConverter found for response type

前端 未结 10 1767
深忆病人
深忆病人 2020-11-30 02:33

Using spring, with this code :

List> messageConverters = restTemplate.getMessageConverters();
for(HttpMessageConverter ht         


        
相关标签:
10条回答
  • 2020-11-30 02:57

    If you can't change server media-type response, you can extend GsonHttpMessageConverter to process additional support types

    public class MyGsonHttpMessageConverter extends GsonHttpMessageConverter {
        public MyGsonHttpMessageConverter() {
            List<MediaType> types = Arrays.asList(
                    new MediaType("text", "html", DEFAULT_CHARSET),
                    new MediaType("application", "json", DEFAULT_CHARSET),
                    new MediaType("application", "*+json", DEFAULT_CHARSET)
            );
            super.setSupportedMediaTypes(types);
        }
    }
    
    0 讨论(0)
  • 2020-11-30 02:58

    You could also simply tell your RestTemplate to accept all media types:

    @Bean
    public RestTemplate restTemplate() {
       final RestTemplate restTemplate = new RestTemplate();
    
       List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
       MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
       converter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
       messageConverters.add(converter);
       restTemplate.setMessageConverters(messageConverters);
    
       return restTemplate;
    }
    
    0 讨论(0)
  • 2020-11-30 03:03

    Try this:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.6.0</version>
    </dependency>
    
    0 讨论(0)
  • 2020-11-30 03:06

    If you are using Spring Boot, you might want to make sure you have the Jackson dependency in your classpath. You can do this manually via:

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </dependency>
    

    Or you can use the web starter:

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    
    0 讨论(0)
提交回复
热议问题