0%

SpringBoot-返回XML

前言

springboot返回的数据默认为json,但是有时间我们想返回xml的时候,就需要做少许修改。

pom.xml文件

引入web和xml依赖。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>com.huzh</groupId>
<artifactId>springboot-xml</artifactId>
<version>1.0-SNAPSHOT</version>

<name>springboot-xml</name>
<description>springboot-xml</description>

<properties>
<java.version>1.8</java.version>
</properties>

<dependencies>
<!--web-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!--xml-->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</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>
</project>

实体类

  • @XmlRootElement的作用
    便于对象与xml文件之间的转换
  • @XmlRootElement的使用
    在java bean上打一个标签@XmlRootElement,也可以使用@XmlRootElement(name = “user”) 。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    @XmlRootElement
    public class User {

    String userName;
    String userAge;
    String userAddress;

    public User(String userName, String userAge, String userAddress) {
    this.userName = userName;
    this.userAge = userAge;
    this.userAddress = userAddress;
    }

    public String getUserName() {
    return userName;
    }

    public void setUserName(String userName) {
    this.userName = userName;
    }

    public String getUserAge() {
    return userAge;
    }

    public void setUserAge(String userAge) {
    this.userAge = userAge;
    }

    public String getUserAddress() {
    return userAddress;
    }

    public void setUserAddress(String userAddress) {
    this.userAddress = userAddress;
    }
    }

    controller类

    启动类默认即可,没有做任何调整。修改@GetMapping属性produces即可。
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    @RestController
    public class UserController {

    @GetMapping(value = "/json", produces = MediaType.APPLICATION_JSON_VALUE)
    public User index() {
    User user = new User("xiaoming", "18", "beijing");
    return user;
    }

    @GetMapping(value = "/xml", produces = MediaType.APPLICATION_XML_VALUE)
    public User XML(){
    User user = new User("xiaoming", "18", "beijing");
    return user;
    }
    }

    测试

  • 访问http://localhost:8080/json
    json.png
  • 访问http://localhost:8080/xml
    xml.png

欢迎关注我的其它发布渠道