Spring rest template cannot convert from text/html
Asked Answered
C

4

7

Spring rest template throws me the following exeption

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class [Lcom.flightsms.core.dto.AirlineResponseDTO;] and content type [text/html;charset=UTF-8]

Here my json response

[
 {
  "airlineId": "1",
  "nameAirline": "American Airlines",
  "codeIataAirline": "AA",
  "iataPrefixAccounting": "1",
  "codeIcaoAirline": "AAL",
  "callsign": "AMERICAN",
  "type": "scheduled",
  "statusAirline": "active",
  "sizeAirline": "963",
  "ageFleet": "10.9",
  "founding": "1934",
  "codeHub": "DFW",
  "nameCountry": "United States",
  "codeIso2Country": "US"
 }
]

dto class

@Data
public class AirlineResponseDTO {
  private String airlineId;
  private String nameAirline;
  private String codeIataAirline;
  private String iataPrefixAccounting;
  private String codeIcaoAirline;
  private String callsign;
  private String type;
  private String statusAirline;
  private String sizeAirline;
  private String ageFleet;
  private String founding;
  private String codeHub;
  private String nameCountry;
  private String codeIso2Country;
}

I suspect that the matter is in the converter. I changed the converter configuration but this did not work

@Configuration
public class MvcConfigSupport extends WebMvcConfigurationSupport {
    protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(converter());
        addDefaultHttpMessageConverters(converters);
    }

    @Bean
    MappingJackson2HttpMessageConverter converter() {
        MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
        converter.setSupportedMediaTypes(Arrays.asList(MediaType.TEXT_HTML));
        return converter;
    }
}
Conglutinate answered 20/7, 2018 at 11:6 Comment(1)
I see that your content-type is text/html;charset=UTF-8; I guess you should create a custom MediaType similar to APPLICATION_JSON_UTF8 that is built by using APPLICATION_JSON_UTF8_VALUESpunky
C
8

Problem solved.

@Bean
public RestTemplate restTemplate() {
    RestTemplate restTemplate = new RestTemplate();
    MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
    converter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_HTML));
    restTemplate.getMessageConverters().add(converter);
    return restTemplate;
}
Conglutinate answered 20/7, 2018 at 13:2 Comment(2)
It seems is my solution? ;-) Glad to know that help.Heulandite
I searched for a solution for over 4 hours. thanks for this.Masterstroke
H
1

Try something like that the default converters are not registered by default and an end user has to be explicit about registering the defaults :

@Configuration
public class WebConfig extends WebMvcConfigurationSupport {

@Bean
public MappingJackson2HttpMessageConverter customJackson2HttpMessageConverter() {
       MappingJackson2HttpMessageConverter jsonConverter = 
       new MappingJackson2HttpMessageConverter();

       ObjectMapper objectMapper = new ObjectMapper(); 

       objectMapper.configure(
       DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES,false);

       jsonConverter.setObjectMapper(objectMapper);
       return jsonConverter;
}

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> 
                                       converters) {
            converters.add(customJackson2HttpMessageConverter());
            super.addDefaultHttpMessageConverters();
}
 } 

Moreover please try fix type exception. Content type [text/html;charset=UTF-8] received from the service, the real content type should be application/json;charset=UTF-8

To get all ContentType you can do your custom converter like that:

List<HttpMessageConverter<?>> messageConverters = 
new ArrayList<HttpMessageConverter<?>>();        

MappingJackson2HttpMessageConverter converter = 
new MappingJackson2HttpMessageConverter();

converter.setSupportedMediaTypes(Arrays.asList({MediaType.ALL}));         
messageConverters.add(converter);  
restTemplate.setMessageConverters(messageConverters);  
Heulandite answered 20/7, 2018 at 11:23 Comment(1)
Try the second part of my answer should help.Heulandite
B
0

Use RestTemplateBuilders to create the resttemplate clients. The builders will add the default Message converters.

Bravar answered 23/12, 2019 at 16:15 Comment(0)
M
0

Using solution above of user9709261 and Knight1128, I am adding the entire spring rest template from which I was able to consume an html response and get the status 200.

HttpHeaders headers = new HttpHeaders();
Charset utf8 = Charset.forName("UTF-8");
MediaType mediaType = new MediaType("text", "html", utf8);
headers.setContentType(mediaType);
HttpEntity<Object> httpEntity = new HttpEntity<>(postResponse, headers);
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(webhookURL);
ApiUrl = new URI(builder.buildAndExpand(urlParams).toUri().toString());

MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.TEXT_HTML));
restTemplate.getMessageConverters().add(converter);

ResponseEntity<String> objectResponseEntity = restTemplate.exchange(ApiUrl, HttpMethod.POST, httpEntity, String.class);

Object body = objectResponseEntity.getBody();
return objectResponseEntity.getStatusCodeValue();




    
Masterstroke answered 26/6, 2023 at 14:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.