当前位置: 代码网 > it编程>编程语言>Java > Springboot集成JAXB返回xml格式

Springboot集成JAXB返回xml格式

2024年12月25日 Java 我要评论
前言springboot可以返回xml数据格式xml数据格式本文使用的是springboot3.4.x版本以上以及jdk17以上,由于在jdk9版本以后javax包被移动并更名为 jakarta,因此

前言

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的资料请关注代码网其它相关文章!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com