2

Consider the following:

@GetMapping("/accounts/{id}")
@ResponseBody
public Account handle() {
    return new Account("1", "sample");
}

There is no Accept header specified in the request, but still the response is by default converted to JSON when Spring Boot is used. The @ResponseBody annotation, in its documentation, doesn't mention anything about there being a conversion

Sagar P. Ghagare
  • 542
  • 2
  • 12
  • 25
Daud
  • 7,429
  • 18
  • 68
  • 115

4 Answers4

1
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

Spring uses Jackson/Json by default (by finding it in the classpath), but you can configure you're own:

@Configuration
public class MixInWebConfig extends WebMvcConfigurationSupport {

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

See HttpMessageConverter API

Essex Boy
  • 7,565
  • 2
  • 21
  • 24
0

Spring-boot applications uses spring-boot-starter-web in dependencies of POM.xml. That particular dependency downloads fasterxmls jackson-datatype which is initialized when we use @springbootapplication.

CodeRider
  • 564
  • 4
  • 15
0

In @requestMapping, You can add variables like Produces or Consumes Eg:

consumes = MediaType.APPLICATION_JSON_VALUE 
produces = MediaType.APPLICATION_JSON_VALUE