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>
實體類配置
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)造器
,因此在實體類加上無參構(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
方法中獲取變量,因此重復了,就異常了,
解決方法如下
更改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時,需要加上這個
produces = MediaType.APPLICATION_XML_VALUE
以上就是Springboot集成JAXB返回xml格式的詳細內(nèi)容,更多關(guān)于Springboot集成JAXB返回xml的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
G1垃圾回收器在并發(fā)場景調(diào)優(yōu)詳解
這篇文章主要為大家介紹了G1垃圾回收器在并發(fā)場景調(diào)優(yōu)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步早日升職加薪2022-04-04SpringBoot+Redis布隆過濾器防惡意流量擊穿緩存
本文主要介紹了SpringBoot+Redis布隆過濾器防惡意流量擊穿緩存,文中根據(jù)實例編碼詳細介紹的十分詳盡,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03@Async導致controller?404及失效原因解決分析
這篇文章主要為大家介紹了@Async導致controller?404失效問題解決,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-07-07詳解使用Spring Security OAuth 實現(xiàn)OAuth 2.0 授權(quán)
本篇文章主要介紹了詳解使用Spring Security OAuth 實現(xiàn)OAuth 2.0 授權(quán),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01SpringBoot四大神器之Actuator的使用小結(jié)
這篇文章主要介紹了SpringBoot四大神器之Actuator的使用小結(jié),詳細的介紹了Actuator的使用和端點的使用,有興趣的可以了解一下2017-11-11