15

I have the following code:

restTemplate.getForObject("http://img.championat.com/news/big/l/c/ujejn-runi_1439911080563855663.jpg", File.class);

I especially took image which doesn't require authorization and available absolutely for all.

when following code executes I see the following stacktrace:

org.springframework.web.client.RestClientException: Could not extract response: no suitable HttpMessageConverter found for response type [class java.io.File] and content type [image/jpeg]
    at org.springframework.web.client.HttpMessageConverterExtractor.extractData(HttpMessageConverterExtractor.java:108)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:559)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:512)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:243)
    at com.terminal.controller.CreateCompanyController.handleFileUpload(CreateCompanyController.java:615)

what do I wrong?

gstackoverflow
  • 36,709
  • 117
  • 359
  • 710

3 Answers3

30

Image is a byte array, so you need to use byte[].class object as a second argument for RestTemplate.getForObject:

String url = "http://img.championat.com/news/big/l/c/ujejn-runi_1439911080563855663.jpg";
byte[] imageBytes = restTemplate.getForObject(url, byte[].class);
Files.write(Paths.get("image.jpg"), imageBytes);

To make it work, you will need to configure a ByteArrayHttpMessageConverter in your application config:

@Bean
public RestTemplate restTemplate(List<HttpMessageConverter<?>> messageConverters) {
    return new RestTemplate(messageConverters);
}

@Bean
public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
    return new ByteArrayHttpMessageConverter();
}

I've tested this in a Spring Boot project and the image is saved to a file as expected.

mzc
  • 3,265
  • 1
  • 20
  • 25
  • The downside if this approach is that the entire file has to be stored in memory. For smaller images that may be fine, but you could still risk blowing the stack. – pioto Aug 19 '15 at 10:57
  • I understood from @gstackoverflow's question that this is the goal - convert response body to an object and go from there. It's not clear if having the file object in memory is acceptable for his use case. By the way, can you clarify on blowing the stack, I'm not sure I follow (AFAIK objects should always be created on the heap, only references can be local) – mzc Aug 19 '15 at 11:10
  • sorry, your right, I meant the heap... but yeah, either way... it's best to use streaming APIs when possible, IMHO, to avoid keeping stuff like this in memory long-term. Since it seems his goal is simply to save the remote file to a local file, I don't see a need for an intermediate `byte[]`? – pioto Aug 19 '15 at 19:00
  • I agree, if you want to handle larger files or large traffic, streaming to a file is the only way. More Spring examples: http://stackoverflow.com/questions/5673260/downloading-a-file-from-spring-controllers – mzc Aug 20 '15 at 06:48
6

If you simply need to get an image from a URL, Java comes with the javax.imageio.ImageIO class, which contains this method signature:

   public static BufferedImage read(URL var0) throws IOException;

example use:

    try {
      BufferedImage image = ImageIO.read(new URL("http://www.foo.com/icon.png"));
      int height = image.getHeight();
      int width = image.getWidth();
    } catch (IOException e) {}
mancini0
  • 4,285
  • 1
  • 29
  • 31
0

The RestTemplate is expecting a class (e.g. some in-memory representation) to convert the response from the server into. For example, it could convert a response like:

{id: 1, name: "someone"}

into a class like:

class NamedThing {
    private int id;
    private String name;

    // getters/setters go here
}

By calling:

NamedThing thing = restTemplate.getForObject("/some/url", NamedThing.class);

But, what you seem to really want to do is to take the response from the server and stream it directly to a file. Various methods exist to get the response body of your HTTP request as something like an InputStream that you can read incrementally, and then write out to an OutputStream (e.g. your file).

This answer shows how to use IOUtils.copy() from commons-io to do some the dirty work. But you need to get an InputStream of your file... A simple way is to use an HttpURLConnection. There's a tutorial with more information.

Community
  • 1
  • 1
pioto
  • 2,472
  • 23
  • 37