Added controller for returning image in response body with configuration via java class
This commit is contained in:
parent
4b18d13857
commit
3601821582
|
@ -1,9 +1,15 @@
|
||||||
package com.baeldung.spring.web.config;
|
package com.baeldung.spring.web.config;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
import org.springframework.core.io.ClassPathResource;
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||||
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||||
import org.springframework.web.servlet.ViewResolver;
|
import org.springframework.web.servlet.ViewResolver;
|
||||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
|
@ -69,4 +75,25 @@ public class WebConfig extends WebMvcConfigurerAdapter {
|
||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void extendMessageConverters(final List<HttpMessageConverter<?>> converters) {
|
||||||
|
converters.add(byteArrayHttpMessageConverter());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
|
||||||
|
final ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
|
||||||
|
arrayHttpMessageConverter.setSupportedMediaTypes(getSupportedMediaTypes());
|
||||||
|
|
||||||
|
return arrayHttpMessageConverter;
|
||||||
|
}
|
||||||
|
|
||||||
|
private List<MediaType> getSupportedMediaTypes() {
|
||||||
|
final List<MediaType> list = new ArrayList<MediaType>();
|
||||||
|
list.add(MediaType.IMAGE_JPEG);
|
||||||
|
list.add(MediaType.IMAGE_PNG);
|
||||||
|
list.add(MediaType.APPLICATION_OCTET_STREAM);
|
||||||
|
|
||||||
|
return list;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package com.baeldung.web.controller;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public class ImageController {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
ServletContext servletContext;
|
||||||
|
|
||||||
|
@RequestMapping(value = "/image-byte-array", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
|
||||||
|
public @ResponseBody byte[] getImageAsByteArray() throws IOException {
|
||||||
|
final InputStream in = servletContext.getResourceAsStream("/WEB-INF/images/image-example.jpg");
|
||||||
|
return IOUtils.toByteArray(in);
|
||||||
|
}
|
||||||
|
}
|
Binary file not shown.
After Width: | Height: | Size: 101 KiB |
Loading…
Reference in New Issue