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

相关推荐
永遠_22 分钟前
YAML格式化工具
java
曼岛_1 小时前
[图解密码学]第4章 古典密码
java·数据库·密码学
一枕槐安4871 小时前
RAG 服务扛住高并发:缓存、队列、限流三件套
java·spring·缓存·异步任务·rag·断路器
Wang's Blog1 小时前
Java 项目实战: 交友项目 - 动态查询开工:昨日回顾与今日任务
java·python·交友
云和数据.ChenGuang1 小时前
光哥说Python 异常深度解析:从异常模型到工程化实践
java·前端·人工智能·python·langchain
SL_staff2 小时前
APS动态重排实践:在多品种小批量场景下实现5分钟插单响应
java·开源·产品
captain3762 小时前
Spring MVC(4)-响应
java·spring boot·后端
Lichee203 小时前
招行Java 面试知识点梳理:Spring 事务、锁、线程池、集合
java·面试
YDS8294 小时前
Small Spring IOC篇:作用域和FactoryBean
java·后端·spring
Shaoshing4 小时前
LeetCode100 42.接雨水
java·算法·leetcode