Поскольку большинство из нас любят oneliners:
Convert.ToBase64String(File.ReadAllBytes(imageFilepath));
Если вам это нужно как массив байтов Base64:
Encoding.ASCII.GetBytes(Convert.ToBase64String(File.ReadAllBytes(imageFilepath)));
В @requestMapping Вы можете добавить переменные, такие как Produces или Consumes, например:
consumes = MediaType.APPLICATION_JSON_VALUE
produces = MediaType.APPLICATION_JSON_VALUE
Spring по умолчанию использует Jackson / Json (найдя его в пути к классам), но вы можете настроить его самостоятельно:
@Configuration
public class MixInWebConfig extends WebMvcConfigurationSupport {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
converters.add(customHttpMessageConverter());
}
}
Приложения Spring-boot используют spring-boot-starter-web в зависимостях POM.xml. Эта конкретная зависимость загружает Джексон-тип данных быстрее xmls, который инициализируется при использовании @springbootapplication.
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>