So i am consuming JSON response from this URL through RestTemplate Link:

"https://s3-ap-southeast-1.amazonaws.com/he-public-data/productdf38641.json"

My Product POJO:

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ProductModel {

    private String uniq_id;
    private String product_name;
    private int retail_price;
    private int discounted_price;
    private List<String> image;
    private String description;
    private String product_rating;
    private String overall_rating;
    private String brand;
}

Now when i use restTemplate to store this Array of Json object in ProductModel[].

ProductModel[] books = restTemplate.getForObject(URL, ProductModel[].class);

I am getting this error

Caused by: org.springframework.web.client.UnknownContentTypeException: Could not extract response: no suitable HttpMessageConverter found for response type [class com.schema.testing.domain.ProductModel] and content type [binary/octet-stream]

when i pass the same JSON object though postman to REST endpoint via POST request. it is able to process that request. IS this all game related to content-type. Please help , what do i have to do next. i am not sure . Any help is appreciated

1

There are 1 best solutions below

0
On

I guess there is a solution for this exception. Try out and let me know the result.
Not any message converter can read your HTTP response, so it fails with an exception. The main problem here is a content-type, In order to overcome this, you can introduce a custom message converter. and register it for all kinds of responses (i.e. ignore the response content-type header). Just like this

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

// 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);