Answer the question
In order to leave comments, you need to log in
Spring mvc - file and json transfer?
I
don’t understand anything in Spring, I just need a simple server for an android application. First, I accumulated it from the Internet
and
the server (on TomCat) began to send json, but I still need to send an image on request, an icon and a big picture
strong)
in order
Config:
@Configuration
@EnableWebMvc
@ComponentScan ("app")
public class WebConfig implements WebMvcConfigurer {
/** если этот раскоментить, то норм получаю Json ответ, но картинка приходит как на фото ниже, наверно это byte[], но не уверен
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
converter.setObjectMapper(new ObjectMapper());
converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));
converters.add(converter);
}
*/
/** если тут, то в браузере сразу вижу фото, но json HTTP Status 500
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(byteArrayHttpMessageConverter());
}
*/
@Bean
public ByteArrayHttpMessageConverter byteArrayHttpMessageConverter() {
ByteArrayHttpMessageConverter arrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
arrayHttpMessageConverter.setSupportedMediaTypes(getSupportedMediaTypes());
return arrayHttpMessageConverter;
}
private List<MediaType> getSupportedMediaTypes() {
List<MediaType> list = new ArrayList<>();
list.add(MediaType.IMAGE_JPEG);
list.add(MediaType.IMAGE_PNG);
list.add(MediaType.APPLICATION_OCTET_STREAM);
return list;
}
..... тут куча @Override которые я не трогал
package app.controller;
import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.ServletContext;
import java.io.IOException;
import java.io.InputStream;
@RestController
public class ImageReturnController {
@Autowired
private ServletContext servletContext;
@RequestMapping(value = "/image_array", method = RequestMethod.GET)
public @ResponseBody byte[] getImageAsByteArray() throws IOException {
InputStream in = servletContext.getResourceAsStream("/WEB-INF/3.jpg");
return IOUtils.toByteArray(in);
}
}
Answer the question
In order to leave comments, you need to log in
one)
@RequestMapping(value = "/image_array/{fileName}", method = RequestMethod.GET)
public @ResponseBody byte[] getImageAsByteArray(@RequestParam("fileName") String fileName) throws IOException {
InputStream in = servletContext.getResourceAsStream("/WEB-INF/"+fileName);
return IOUtils.toByteArray(in);
}
If I were you, I would look towards Spring Boot. In it, such trifles are already configured to cover standard needs.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question