Springboot集成JAXB返回xml格式
前言
Springboot可以返回xml數(shù)據(jù)格式
xml數(shù)據(jù)格式
本文使用的是Springboot3.4.x版本以上以及jdk17以上,由于在JDK9版本以后javax包被移動并更名為 jakarta,因此使用包如下
<!-- https://mvnrepository.com/artifact/jakarta.xml.bind/jakarta.xml.bind-api -->
<dependency>
<groupId>jakarta.xml.bind</groupId>
<artifactId>jakarta.xml.bind-api</artifactId>
<version>4.0.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.glassfish.jaxb/jaxb-runtime -->
<dependency>
<groupId>org.glassfish.jaxb</groupId>
<artifactId>jaxb-runtime</artifactId>
<version>4.0.5</version>
</dependency>
實(shí)體類配置
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class User {
@XmlElement
private Long id;
@XmlElement
private String userName;
@XmlElement
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User(Long id, String userName, Integer age) {
this.id = id;
this.userName = userName;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + ''' +
", age=" + age +
'}';
}
}接口層配置
@RestController
public class UserController {
@GetMapping(value = "/getJson", produces = MediaType.APPLICATION_JSON_VALUE)
public User getJson() {
User user = new User(2L, "asdas1", 15);
return user;
}
@GetMapping(value = "/getXml", produces = MediaType.APPLICATION_XML_VALUE)
public User getXml() {
User user = new User(1L, "hello", 12);
return user;
}
}
xml返回
一開始,調(diào)用
http://ip:端口/getXml
拋出異常,異常如下

后面調(diào)試源碼異常信息,發(fā)現(xiàn)需要一個無參構(gòu)造器,因此在實(shí)體類加上無參構(gòu)造函數(shù)
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class User {
@XmlElement
private Long id;
@XmlElement
private String userName;
@XmlElement
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User() {
}
public User(Long id, String userName, Integer age) {
this.id = id;
this.userName = userName;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + ''' +
", age=" + age +
'}';
}
}然后調(diào)用接口發(fā)覺還是異常

然后發(fā)覺異常信息為

發(fā)覺該包會從成員變量以及get方法中獲取變量,因此重復(fù)了,就異常了,
解決方法如下
更改get方法名稱
import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class User {
@XmlElement
private Long id;
@XmlElement
private String userName;
@XmlElement
private Integer age;
public Long getId1() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName1() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge1() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User() {
}
public User(Long id, String userName, Integer age) {
this.id = id;
this.userName = userName;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + ''' +
", age=" + age +
'}';
}
}訪問之后返回

使用@XmlAccessorType
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class User {
@XmlElement
private Long id;
@XmlElement
private String userName;
@XmlElement
private Integer age;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User() {
}
public User(Long id, String userName, Integer age) {
this.id = id;
this.userName = userName;
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + ''' +
", age=" + age +
'}';
}
}在類上加上 @XmlAccessorType(XmlAccessType.FIELD) 注解,加上此注解后,xml的訪問類型為成員變量,而不是getter/setter方法

備注: 返回xml時(shí),需要加上這個
produces = MediaType.APPLICATION_XML_VALUE

以上就是Springboot集成JAXB返回xml格式的詳細(xì)內(nèi)容,更多關(guān)于Springboot集成JAXB返回xml的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
深入探究MyBatis插件機(jī)制靈活擴(kuò)展及自定義增強(qiáng)框架能力
這篇文章主要介紹了深入探究MyBatis插件機(jī)制靈活擴(kuò)展及自定義增強(qiáng)框架能力2024-01-01
java 實(shí)現(xiàn)截取字符串并按字節(jié)分別輸出實(shí)例代碼
這篇文章主要介紹了java 實(shí)現(xiàn)截取字符串并按字節(jié)分別輸出實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下2017-03-03
Mybatis如何解決sql中l(wèi)ike通配符模糊匹配問題
這篇文章主要介紹了Mybatis如何解決sql中l(wèi)ike通配符模糊匹配問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Java Mybatis框架多表操作與注解開發(fā)詳解分析
MyBatis 是一款優(yōu)秀的持久層框架,它支持自定義 SQL、存儲過程以及高級映射。MyBatis 免除了幾乎所有的 JDBC 代碼以及設(shè)置參數(shù)和獲取結(jié)果集的工作。MyBatis 可以通過簡單的 XML 或注解來配置和映射原始類型、接口和 Java POJO為數(shù)據(jù)庫中的記錄2021-10-10
SpringBoot+Caffeine+Redis實(shí)現(xiàn)多級緩存的方法
多級緩存是指在系統(tǒng)中部署多層功能互補(bǔ)的緩存組件,讓請求按照預(yù)設(shè)順序依次訪問各層緩存,僅當(dāng)所有緩存層均未命中時(shí),才訪問底層數(shù)據(jù)庫的架構(gòu)模式,這篇文章主要介紹了SpringBoot+Caffeine+Redis實(shí)現(xiàn)多級緩存的方法,需要的朋友可以參考下2026-02-02
Java服務(wù)剛啟動時(shí)接口超時(shí)排查全過程
這篇文章主要為大家介紹了Java服務(wù)剛啟動時(shí),一小波接口超時(shí)排查全過程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07

