0

first I have to say, that I am pretty new to Springs RestTemplate.

I am trying to receive data from the imdb-api. (For example http://imdbapi.org/?title=Avatar&type=xml) Therefore I am using Springs RestTemplate. But:

  1. the webservice returns the data as application/octet-stream (even I declared that I want it as xml (when I browse the site with my browser I get the data as text/xml))
  2. RestTemplate doesn't find my declared ByteArrayMessageConverter (to convert application/octet-stream)

I realy don't know where my mistakes are.

Here is the code to initialise the restTemplate:

public void onInit() {
    _log.debug("Setting up the Spring Resttemplate");

    _restTemplate = new RestTemplate();
    List<HttpMessageConverter<?>> list = new ArrayList<HttpMessageConverter<?>>();
    list.add(new SourceHttpMessageConverter<Source>());
    list.add(new ByteArrayHttpMessageConverter());
    _restTemplate.setMessageConverters(list);

    _log.debug("Setting up the HTTP Headers for Restrequest");
    List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
    _log.trace("allow {}", MediaType.APPLICATION_XML_VALUE);
    acceptableMediaTypes.add(MediaType.APPLICATION_XML);
    _log.trace("allow {}", MediaType.TEXT_HTML_VALUE);
    acceptableMediaTypes.add(MediaType.TEXT_XML);
    _log.trace("set accepted charset to uft-8");
    List<Charset> acceptableCharsets = new ArrayList<Charset>();
    acceptableCharsets.add(Charset.forName("utf-8"));

    _httpHeaders = new HttpHeaders();
    _httpHeaders.set("User-Agent", "something"); //only a user-agent, because the api returns a 403 if it is not set
    _httpHeaders.setAcceptCharset(acceptableCharsets);
    _httpHeaders.setAccept(acceptableMediaTypes);
}

Here is the code with the call:

_log.info("connect to Imdb-Webservice {}", _imbdWebserviceBaseUrl);

    Map<String, Object> uriVariables = new HashMap<String, Object>();
    uriVariables.put("title", pTitle);

    ResponseEntity<Source> response = _restTemplate.exchange(_imbdWebserviceBaseUrl, HttpMethod.GET, new HttpEntity<String>(_httpHeaders), Source.class, uriVariables);

_imbdWebserviceBaseUrl is set to http://imdbapi.org/?title={title}&type=xml

Then I am getting this error message:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [interface javax.xml.transform.Source] and content type [application/octet-stream]
at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:107)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:687)
at org.springframework.web.client.RestTemplate$ResponseEntityResponseExtractor.extractData(RestTemplate.java:673)
at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:491)
at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:454)
at org.springframework.web.client.RestTemplate.exchange(RestTemplate.java:401)
at my.domain.projectname.integrationimpl.WebserviceHelper.getXml(WebserviceHelper.java:131)

Thanks for your help

chresse
  • 5,486
  • 3
  • 30
  • 47

1 Answers1

2

the web service returns the data as application/octet-stream (even I declared that I want it as xml (when I browse the site with my browser I get the data as text/xml))

As far as I can see this rest service is not giving back the correct Content-Type (text/xml or similar). If your browser renders it correctly that's probably Chrome or Firefox, but IE will just show you html-ish kind of output.

RestTemplate doesn't find my declared ByteArrayMessageConverter (to convert application/octet-stream)

Well you are asking for a Source as far as I can see:

ResponseEntity<Source> response = _restTemplate.exchange(_imbdWebserviceBaseUrl, HttpMethod.GET, new HttpEntity<String>(_httpHeaders), Source.class, uriVariables);

The MessageConverters themselves have a method that determines if this converter is applicable, for ByteArrayHttpMessageConverter this is:

@Override
public boolean supports(Class<?> clazz) {
    return byte[].class.equals(clazz);
}

Since you are asking for a Source.class it wont use this converter.

Auke
  • 534
  • 5
  • 16