在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在后:

相关推荐
太过平凡的小蚂蚁21 小时前
适配器模式:让不兼容的接口协同工作
java·前端·javascript
ljh_learn_from_base21 小时前
【spring boot 使用apache poi 生成和处理word 文档】
java·spring boot·word·apache
数字芯片实验室1 天前
流片可以失败,但人心的账本不能亏空
java·开发语言
华仔啊1 天前
为什么你的 @Transactional 不生效?一文搞懂 Spring 事务机制
java·后端
Lacrimosa&L1 天前
OS_3 Memory、4 File、5 IO
java
爱学的小码1 天前
JavaEE——多线程1(超详细版)
java·java-ee
tuokuac1 天前
依赖spring-cloud-starter-gateway与spring-cloud-gateway-dependencies的区别
java·gateway
seabirdssss1 天前
JDK 11 环境正确,端口未被占用,但是运行 Tomcat 11 仍然闪退
java·开发语言·tomcat
努力学算法的蒟蒻1 天前
day03(11.1)——leetcode面试经典150
java·算法·leetcode
Mr YiRan1 天前
SYN关键字辨析,各种锁优缺点分析和面试题讲解
java·开发语言