前言
springboot可以返回xml
数据格式
xml数据格式
本文使用的是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返回
一开始,调用
http://ip:端口/getxml
抛出异常,异常如下
后面调试源码异常信息,发现需要一个无参构造器
,因此在实体类加上无参构造函数
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 + '}'; } }
然后调用接口发觉还是异常
然后发觉异常信息为
发觉该包会从成员变量以及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格式的详细内容,更多关于springboot集成jaxb返回xml的资料请关注代码网其它相关文章!
发表评论