Springboot集成JAXB返回xml格式
前言
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的资料请关注脚本之家其它相关文章!
相关文章
Java SpringBoot快速集成SpringBootAdmin管控台监控服务详解
这篇文章主要介绍了如何基于springboot-admin管控台监控服务,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下2021-09-09mybatis 通过拦截器打印完整的sql语句以及执行结果操作
这篇文章主要介绍了mybatis 通过拦截器打印完整的sql语句以及执行结果操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2020-10-10SpringBoot集成Spring Security的方法
Spring security,是一个强大的和高度可定制的身份验证和访问控制框架。这篇文章主要介绍了SpringBoot集成Spring Security的操作方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下2020-07-07
最新评论