java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Springboot集成JAXB返回xml

Springboot集成JAXB返回xml格式

作者:考虑考虑

这篇文章主要为大家详细介绍了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的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文