0

I'm an immigrant from JAX-RS to Spring.

With my following code,

@RequestMapping(method = RequestMethod.GET,
                produces = {MediaType.APPLICATION_JSON_VALUE,
                            MediaType.APPLICATION_XML_VALUE})
public @ResponseBody ResponseEntity<List<Item>> read() {
    final List<Item> body
            = range(0, current().nextInt(1, 10))
            .mapToObj(i -> Item.newRandomInstance())
            .collect(toList());
    return ResponseEntity.ok(body);
}

I get 200 for application/json but 406 for application/xml.

What is the best way to solve this problem?

Is there any functionality for automatic plural wrapping?

Here comes my dependencies

dependencies {
    compile("org.springframework.boot:spring-boot-starter-web") {
        //    exclude module: "spring-boot-starter-tomcat"
    }
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    //  compile("org.springframework.boot:spring-boot-starter-jetty")
    compile("org.springframework.boot:spring-boot-starter-actuator")
    testCompile('org.springframework.boot:spring-boot-starter-test')
    compile("org.springframework.boot:spring-boot-configuration-processor")
    testCompile('com.jayway.jsonpath:json-path') // for gs-rest-service
    // ----------------------------------------------------------------------------------------------- springfox-swagger
    compile 'io.springfox:springfox-swagger2:2.7.0'
    compile 'io.springfox:springfox-swagger-ui:2.7.0'
    //compile('org.springframework.boot:spring-boot-starter-amqp') // rabbitmq
}
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184
  • Do you have both json and xml message converter? are you sending requests with different Accept header (application/xml, application/json)? – Dmitry Senkovich Sep 26 '17 at 11:55
  • @DmitrySenkovich Yes, I'm testing using curl. – Jin Kwon Sep 26 '17 at 11:57
  • I'm also using `spring-boot-starter-web`. I've checked that it configures no `HttpMessageConverter`'s by default. So it seems like you have json converter while forgot to configure for xml – Dmitry Senkovich Sep 27 '17 at 07:48

1 Answers1

0

I'm putting my own answer for others.

I added following dependency.

// for application/xml 
// remove if you don't need to deal with XML.
runtime 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.1' 
Jin Kwon
  • 20,295
  • 14
  • 115
  • 184