本文介绍了具有XML支持的Spring Boot REST的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Spring Boot 1.2.5创建了一个简单的REST Web服务,它适用于JSON,但我不能让这个工作返回XML。

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.

这是我的控制器:

@RestController
..
@RequestMapping(method = RequestMethod.GET,  produces = {MediaType.APPLICATION_JSON_VALUE, MediaType.APPLICATION_XML_VALUE})
@ResponseStatus(HttpStatus.OK)
public List<Activity> getAllActivities() {
    return activityRepository.findAllActivities();
}

当我用调用它时接受:application / json 一切正常,但当我尝试使用 application / xml 时,我得到了一些带有406错误和消息的HTML:

When I call it with Accept: application/json everything works, but when I try with application/xml I get some HTML with 406 Error and message:

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但它没有帮助。

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.

推荐答案

使这个工作在Spring Boot中没有使用Jersey我们需要添加这种依赖:

To make this work in Spring Boot without using Jersey we need to add this dependency:

<dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-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>

这是很好的教程:

这篇关于具有XML支持的Spring Boot REST的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 03:46
查看更多