我用Spring Boot 1.2.5做了简单的休息webservice,它对json很好,但是我不能让这个工作返回xml。支持XML的Spring Boot REST
这是我的控制器:
@RestController
..
@RequestMapping(method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
@ResponseStatus(HttpStatus.OK)
public List<Activity> getAllActivities() {
return activityRepository.findAllActivities();
}
当我把它与接受:应用/ JSON一切正常,但是当我与应用程序/ xml的尝试,我得到一些HTML与406错误和消息:
The resource identified by this request is only capable of generating responses
with characteristics not acceptable according to the request "accept" headers.
我的模型对象:
@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...
}
我的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>
我是否需要一些额外的罐子在我的pom.xml,以使这项工作?我试图添加jaxb-api或者jax-impl,但它没有帮助。
你确定你将它设置为'应用程序/ xml'而不是别的?启用调试日志记录并查看里面发生了什么。 –
你能分享你的POM文件吗! – Avis
@Avis我粘贴我的pom.xml – jgr