2017-09-26 3 views
0

Ich bin ein Einwanderer von JAX-RS zu Spring.Wie kann ich eine Liste von Elementen in der Anwendung/xml mit Spring antworten?

Mit meinem folgenden 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); 
} 

ich 200 für application/json aber 406 für application/xml.

Was ist der beste Weg, um dieses Problem zu lösen?

Gibt es Funktionen für die automatische Mehrfachverpackung?

kommt meine Abhängigkeiten

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 
} 
+0

Haben Sie beide JSON und XML-Nachrichtenkonverter? Senden Sie Anfragen mit unterschiedlichen Accept-Header (application/xml, application/json)? –

+0

@DmitrySenkovich Ja, ich teste mit Curl. –

+0

Ich benutze auch 'Spring-Boot-Starter-Web'. Ich habe überprüft, dass es standardmäßig keine 'HttpMessageConverter 'konfiguriert. So scheint es, als ob Sie JSON-Konverter haben, während vergessen wurde, für XML zu konfigurieren –

Antwort

0

Ich bin für andere meine eigene Antwort setzen.

Ich habe folgende Abhängigkeit hinzugefügt.

// for application/xml 
// remove if you don't need to deal with XML. 
runtime 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.9.1' 
Verwandte Themen