N
N
Neonoviiwolf2018-02-27 01:26:40
Spring
Neonoviiwolf, 2018-02-27 01:26:40

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 которые я не трогал

How can I combine these configureMessageConverters or what can be done with them to make everything work, or if it is a byte[], can I convert it back on the client side?
5a94882f91381336988879.jpeg
Second
@RestController
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);
    }
}

I have a strictly specified image returned here, but how can I rewrite it so that I send the "name".jpg that I need? Those. comes name = 100500 and I get "/WEB-INF/100500.jpg" if exists
AND last
InputStream in = servletContext.getResourceAsStream("/WEB-INF/3.jpg"); here I set the address, but from the beginning I wanted D:\Android\testDB - it didn’t work, how can this be passed to him?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Y
Yerlan Ibraev, 2018-02-27
@Neonoviiwolf

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

2) Read what the classpath is until full enlightenment.

K
Kirill Romanov, 2018-02-27
@Djaler

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 question

Ask a Question

731 491 924 answers to any question