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

相关推荐
s_w.h11 分钟前
【 linux 】线程互斥与同步
java·linux·服务器·开发语言
学长毕业设计12 分钟前
基于SpringBoot的校园二手物品交易系统(源码+文档+讲解视频)
java·spring boot·后端
观无23 分钟前
若依AOP
java
是2的10次方啊25 分钟前
日志不是越多越好:开发优化、常见坑与线上排查命令
java
ofoxcoding31 分钟前
Claude Fable 5.1 API 接入指南:模型标识、五级定价策略、effort 参数与缓存 TTL 配置详解
java·spring·缓存·ai
zww89491111 小时前
相册印刷项目实战指南:从设计到成品的完整技术流程
java·小程序·eclipse
程序猫.1 小时前
双指针问题
java·数据结构·算法
拾光Ծ1 小时前
【MySQL】复合查询、表的连接
java·数据库·sql·mysql
MetaLite1 小时前
Java通用枚举驱动下拉框-元数据接口与前端契约
java·前端·状态模式
文子越来越强1 小时前
线程池使用总结
java