SpringBoot返回json和xml的示例代码

时间:2022-04-25 03:02:48

有些情况接口需要返回的是xml数据,在springboot中并不需要每次都转换一下数据格式,只需做一些微调整即可。

新建一个springboot项目,加入依赖jackson-dataformat-xml,pom文件代码如下:

?
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>
 
  <groupid>com.dalaoyang</groupid>
  <artifactid>springboot_xml</artifactid>
  <version>0.0.1-snapshot</version>
  <packaging>jar</packaging>
 
  <name>springboot_xml</name>
  <description>springboot_xml</description>
 
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>1.5.9.release</version>
    <relativepath/> <!-- lookup parent from repository -->
  </parent>
 
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <project.reporting.outputencoding>utf-8</project.reporting.outputencoding>
    <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-devtools</artifactid>
      <scope>runtime</scope>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>
 
    <dependency>
      <groupid>com.fasterxml.jackson.dataformat</groupid>
      <artifactid>jackson-dataformat-xml</artifactid>
    </dependency>
  </dependencies>
 
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
</project>

启动类默认即可,没有做任何调整。

新建一个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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.dalaoyang.entity;
 
import javax.xml.bind.annotation.xmlelement;
import javax.xml.bind.annotation.xmlrootelement;
 
/**
 * @author dalaoyang
 * @description
 * @project springboot_learn
 * @package com.dalaoyang.entity
 * @email yangyang@dalaoyang.cn
 * @date 2018/4/8
 */
@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;
  }
 
  @xmlelement
  public string getusername() {
    return username;
  }
 
  public void setusername(string username) {
    this.username = username;
  }
 
  @xmlelement
  public string getuserage() {
    return userage;
  }
 
  public void setuserage(string userage) {
    this.userage = userage;
  }
 
  @xmlelement
  public string getuseraddress() {
    return useraddress;
  }
 
  public void setuseraddress(string useraddress) {
    this.useraddress = useraddress;
  }
}

最后是controller,代码如下:

?
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
package com.dalaoyang.controller;
import com.dalaoyang.entity.user;
import org.springframework.http.mediatype;
import org.springframework.web.bind.annotation.getmapping;
import org.springframework.web.bind.annotation.restcontroller;
/**
 * @author dalaoyang
 * @description
 * @project springboot_learn
 * @package com.dalaoyang.controller
 * @email yangyang@dalaoyang.cn
 * @date 2018/4/8
 */
@restcontroller
public class usercontroller {
 
  //http://localhost:8080/json
  @getmapping(value = "/json",produces = mediatype.application_json_value)
  public user index(){
    user user = new user("dalaoyang", "26", "北京");
    return user;
  }
 
  //http://localhost:8080/xml
  @getmapping(value = "/xml",produces = mediatype.application_xml_value)
  public user xml(){
    user user = new user("dalaoyang", "26", "北京");
    return user;
  }
}

到这里就可以启动项目了,访问http://localhost:8080/json,可以看到如下图

SpringBoot返回json和xml的示例代码

访问http://localhost:8080/xml,如下图

SpringBoot返回json和xml的示例代码

源码下载 :https://gitee.com/dalaoyang/springboot_learn

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://www.jianshu.com/p/231b5ffa19b7