Как Spring знает, как преобразовать возвращаемый объект в JSON, а не XML или какой-либо другой формат?

Поскольку большинство из нас любят oneliners:

Convert.ToBase64String(File.ReadAllBytes(imageFilepath));

Если вам это нужно как массив байтов Base64:

Encoding.ASCII.GetBytes(Convert.ToBase64String(File.ReadAllBytes(imageFilepath)));
2
задан Sagar P. Ghagare 16 January 2019 в 11:21
поделиться

4 ответа

В @requestMapping Вы можете добавить переменные, такие как Produces или Consumes, например:

consumes = MediaType.APPLICATION_JSON_VALUE 
produces = MediaType.APPLICATION_JSON_VALUE
0
ответ дан Swapnil Kashid 16 January 2019 в 11:21
поделиться

Spring по умолчанию использует Jackson / Json (найдя его в пути к классам), но вы можете настроить его самостоятельно:

@Configuration
public class MixInWebConfig extends WebMvcConfigurationSupport {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(customHttpMessageConverter());
    }
}

См. HttpMessageConverter API

0
ответ дан Essex Boy 16 January 2019 в 11:21
поделиться

Приложения Spring-boot используют spring-boot-starter-web в зависимостях POM.xml. Эта конкретная зависимость загружает Джексон-тип данных быстрее xmls, который инициализируется при использовании @springbootapplication.

0
ответ дан CodeRider 16 January 2019 в 11:21
поделиться
By default, A controller return JSON on spring boot project. But If you want XML format then you can configure this on the pom.xml. For example, you can add this following dependency if you want to return XML data,

<dependency>
   <groupId>com.fasterxml.jackson.dataformat</groupId>
   <artifactId>jackson-dataformat-xml</artifactId>
</dependency>
0
ответ дан Sayed Mahmud Raihan 16 January 2019 в 11:21
поделиться
Другие вопросы по тегам:

Похожие вопросы: