Mapping external API json response value from a redirected html url to POJO

187 Views Asked by At

I am trying to get a json response value from a url : http://coverartarchive.org/release-group/2a0981fb-9593-3019-864b-ce934d97a16e

which redirects to : https://ia800300.us.archive.org/0/items/mbid-cc8a35fc-ef70-40b5-b410-c1b5a029a866/index.json

I want to map the images value to an arraylist , but I get this exception :

org.springframework.web.client.RestClientException: Error while extracting response for type [class com.mashup.entity.coverart.Image] and content type [text/html]; nested exception is org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false'); nested exception is com.fasterxml.jackson.core.JsonParseException: Unexpected character ('<' (code 60)): expected a valid value (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

In my CoverArtService, I have set HttpMessageConverter :

 public static Image fetchCoverArtImageUrl(String mbid) {
    restTemplate = new RestTemplate();
    restTemplate.setMessageConverters(MessageConverterService.getMessageConverters());


    image = restTemplate.getForObject(COVER_ART_URL + mbid, Image.class);

    return image;
  }

My MessageConverterService :

   public class MessageConverterService {
    
      private static ObjectMapper objectMapper;
    
      public static ObjectMapper createObjectMapper () {
      objectMapper = new ObjectMapper()
            .registerModule(new Jdk8Module())
            .registerModule(new JavaTimeModule())
            .configure(
                SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
        return objectMapper;
      }
    
      public static ObjectMapper getObjectMapper() {
        return objectMapper;
      }
    
       public static List<HttpMessageConverter<?>> getMessageConverters () {
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
    MappingJackson2HttpMessageConverter jsonMessageConverter = new MappingJackson2HttpMessageConverter();
    jsonMessageConverter.setObjectMapper(createObjectMapper());
    jsonMessageConverter.setSupportedMediaTypes(Collections.singletonList(MediaType.ALL));
    messageConverters.add(jsonMessageConverter);


    return messageConverters;
  }

What is the issue here, since I already set a HttpMessageConverter?
The original url works in postman, but it gets redirected in browsers.

Thanks!

0

There are 0 best solutions below