具有 XML 支持的 Spring Boot REST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32654291/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot REST with XML Support
提问by jgr
I made a simple REST webservice with Spring Boot 1.2.5 and it works fine for JSON but I can't make this work to return XML.
我使用 Spring Boot 1.2.5 制作了一个简单的 REST web 服务,它适用于 JSON,但我无法使这项工作返回 XML。
This is my controller:
这是我的控制器:
@RestController
..
@RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
@ResponseStatus(HttpStatus.OK)
public List<Activity> getAllActivities() {
return activityRepository.findAllActivities();
}
When I call it with Accept: application/jsoneverything works, but when I try with application/xmlI get some HTML with 406 Error and message:
当我调用它时Accept: application/json一切正常,但是当我尝试使用时,application/xml我得到一些带有 406 错误和消息的 HTML:
The resource identified by this request is only capable of generating responses
with characteristics not acceptable according to the request "accept" headers.
My model objects:
我的模型对象:
@XmlRootElement
public class Activity {
private Long id;
private String description;
private int duration;
private User user;
//getters & setters...
}
@XmlRootElement
public class User {
private String name;
private String id;
//getters&setters...
}
My pom.xml
我的 pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
Do I need some additional jars in my pom.xml to make this work? I tried adding jaxb-api or jax-impl but it didn't help.
我需要在我的 pom.xml 中添加一些额外的 jars 来完成这项工作吗?我尝试添加 jaxb-api 或 jax-impl 但没有帮助。
回答by jgr
To make this work in Spring Boot without using Jersey we need to add this dependency:
为了在不使用 Jersey 的情况下在 Spring Boot 中完成这项工作,我们需要添加这个依赖项:
<dependency>
<groupId>com.fasterxml.Hymanson.dataformat</groupId>
<artifactId>Hymanson-dataformat-xml</artifactId>
</dependency>
The output will be a bit ugly but it works:
输出会有点难看,但它有效:
<ArrayList
xmlns="">
<item>
<id/>
<description>Swimming</description>
<duration>55</duration>
<user/>
</item>
<item>
<id/>
<description>Cycling</description>
<duration>120</duration>
<user/>
</item>
</ArrayList>
Here is nice tutorial: http://www.javacodegeeks.com/2015/04/jax-rs-2-x-vs-spring-mvc-returning-an-xml-representation-of-a-list-of-objects.html
这是很好的教程:http: //www.javacodegeeks.com/2015/04/jax-rs-2-x-vs-spring-mvc-returning-an-xml-representation-of-a-list-of-objects .html
回答by Subhasish Sahu
We can achieve this as below :
我们可以实现如下:
Code
代码
package com.subu;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.xml.bind.annotation.*;
@Entity
@XmlRootElement(name = "person")
@Table(name="person")
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@XmlAttribute(name = "first-name")
private String first_name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirst_name() {
return first_name;
}
public void setFirst_name(String first_name) {
this.first_name = first_name;
}
public String getLast_name() {
return last_name;
}
public void setLast_name(String last_name) {
this.last_name = last_name;
}
public String getDate_of_birth() {
return date_of_birth;
}
public void setDate_of_birth(String date_of_birth) {
this.date_of_birth = date_of_birth;
}
@XmlAttribute(name = "last-name")
private String last_name;
@XmlAttribute(name = "dob")
private String date_of_birth;
}
@RestController
public class PersonController {
@Autowired
private PersonRepository personRepository;
@RequestMapping(value = "/persons/{id}", method = RequestMethod.GET,produces={MediaType.APPLICATION_XML_VALUE},headers = "Accept=application/xml")
public ResponseEntity<?> getPersonDetails(@PathVariable Long id, final HttpServletRequest request)throws Exception {
Person personResponse=personRepository.findPersonById(id);
return ResponseEntity.ok(personResponse);
}
}
package com.subu;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.web.SpringBootServletInitializer;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication
@Configuration
@ComponentScan
@EnableAutoConfiguration
@EnableScheduling
public class Application extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
private static Class<Application> applicationClass = Application.class;
}


