在Java代码中指定用JAXB的XmlElement注解的元素的顺序

例如,下面的类RegisterResponse 使用了XmlRootElement注解,同时也使用XmlType注解,并用XmlType注解的propOrder属性,指定了两个用XmlElement注解的元素出现的顺序,先出现flag,后出现enterpriseId(在xml中的元素名称是body):

csharp 复制代码
package com.thb.server.register;

import jakarta.xml.bind.annotation.XmlElement;
import jakarta.xml.bind.annotation.XmlRootElement;
import jakarta.xml.bind.annotation.XmlType;

/**
 * 该类映射到http响应的xml
 * @author thb
 *
 */
//使用了JAXB注解,映射到xml中的response元素
@XmlRootElement(name = "response")
@XmlType(propOrder = {"flag", "enterpriseId"})
public class RegisterResponse {
    private String flag;
    private String enterpriseId;

    public RegisterResponse() {}

    //使用了JAXB注解,映射到xml中的flag元素
    @XmlElement(name = "flag", required = true)
    public String getFlag() {
        return this.flag;
    }

    public void setFlag(String flag) {
        this.flag = flag;
    }

    //使用了JAXB注解,映射到xml中的body元素
    @XmlElement(name = "body", required = true)
    public String getEnterpriseId() {
        return this.enterpriseId;
    }

    public void setEnterpriseId(String enterpriseId) {
        this.enterpriseId = enterpriseId;
    }

}

生成XML schema,显示顺序是flag在前,body在后:

用Postman发送http请求,得到的响应是flag在前,body在后:

作为对比,如果上面的类不使用XmlType注解,即变为下面这样:

csharp 复制代码
@XmlRootElement(name = "response")
public class RegisterResponse {
...

生成XML schema,是body在前,flag在后:

用Postman发送http请求,得到的响应是是body在前,flag在后:

相关推荐
达文汐12 分钟前
【困难】力扣算法题解析LeetCode332:重新安排行程
java·数据结构·经验分享·算法·leetcode·力扣
培风图南以星河揽胜13 分钟前
Java版LeetCode热题100之零钱兑换:动态规划经典问题深度解析
java·leetcode·动态规划
启山智软36 分钟前
【中大企业选择源码部署商城系统】
java·spring·商城开发
我真的是大笨蛋39 分钟前
深度解析InnoDB如何保障Buffer与磁盘数据一致性
java·数据库·sql·mysql·性能优化
怪兽源码1 小时前
基于SpringBoot的选课调查系统
java·spring boot·后端·选课调查系统
恒悦sunsite1 小时前
Redis之配置只读账号
java·redis·bootstrap
梦里小白龙1 小时前
java 通过Minio上传文件
java·开发语言
人道领域1 小时前
javaWeb从入门到进阶(SpringBoot事务管理及AOP)
java·数据库·mysql
sheji52612 小时前
JSP基于信息安全的读书网站79f9s--程序+源码+数据库+调试部署+开发环境
java·开发语言·数据库·算法
毕设源码-邱学长2 小时前
【开题答辩全过程】以 基于Java Web的电子商务网站的用户行为分析与个性化推荐系统为例,包含答辩的问题和答案
java·开发语言