web-services – 使用JAXB和JAXWS Annotations将枚举属性编组到XML中

web-services – 使用JAXB和JAXWS Annotations将枚举属性编组到XML中,第1张

概述假设我们有以下 Java 1.5枚举: import javax.xml.bind.annotation.XmlAccessType;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlElement;@XmlAccessorType(XmlAccessType.FIELD 假设我们有以下 Java 1.5枚举:
import javax.xml.bind.annotation.XmlAccesstype;import javax.xml.bind.annotation.XmlAccessorType;import javax.xml.bind.annotation.XmlElement;@XmlAccessorType(XmlAccesstype.FIELD)public enum ReturnCode {    OK(0,"Ok"),ERROR_VALIDATION(1,"ValIDation Error"),ERROR_TRANSPORT(2,"Transport Error"),ERROR_CASE_01(101,"Business situation #01"),ERROR_CASE_02(102,"Business situation #02"),ERROR_CASE_03(103,"Business situation #03");    @XmlElement(nillable=false,required=true)    private Integer code = 0;    @XmlElement(nillable=false,required=true)    private String message = null;    private ReturnCode(Integer code,String message) {        this.code = code;        this.message = message;    }    public Integer getCode() {        return code;    }    public String getMessage() {        return message;    }}

我正在使用Apache CXF和生成的WSDL,正如预期的那样,将上述枚举转换为限制:

<xsd:simpleType name="ReturnCode">    <xsd:restriction base="xsd:string">        <xsd:enumeration value="OK"/>        <xsd:enumeration value="ERROR_VALIDATION"/>        <xsd:enumeration value="ERROR_TRANSPORT"/>        <xsd:enumeration value="ERROR_CASE_01"/>        <xsd:enumeration value="ERROR_CASE_02"/>        <xsd:enumeration value="ERROR_CASE_03"/>    </xsd:restriction></xsd:simpleType>

到目前为止这么好,这是一个理想的功能.我自己记得在Apache CXF之前与这样的结构挣扎(当我使用XFire时).

但是,这不是这种情况.我想产生不同的结果.我希望将枚举转换为复杂类型,并且在编组包含此枚举实例的对象时,将属性代码和消息都转换为XML元素.我只希望它不像enum那样表现.我知道如果我使用普通类而不是枚举,我可以实现这一目标.但是,我非常希望保持枚举,所以我在代码的java部分保持类型安全.

如果生成的WSDL仍然可能对可能的值有限制,那么它将是完美的场景.但是,我可以没有它.这里最重要的是保持Java 1.5枚举,同时仍然编组(并生成WSDL)ReturnCode作为复杂类型,代码和消息作为其元素.

我尝试用枚举源代码中的给定JAXWS Annotations提示.是否有可能通过那些(或其他一些)注释来实现这一点?或者我是否必须编写自定义marshaller / unmarshaller和WSDL生成器?

非常感谢你!

最好的祝福,

Filipe Fedalto

解决方法 在您的java服务器代码中使用枚举,并在您的服务接口中转换为复杂类型.

例:

@WebMethodpublic ComplexType GetInfo(){  ReturnCode response;  response = ReturnCode.OK;  ComplexType wsResponse;  wsResponse = response.toComplexType()  return wsResponse;}@WebMethodpublic voID PutInfo(ComplexType input){  ReturnCode request = ReturnCode.fromComplexType(input);  //more code}
总结

以上是内存溢出为你收集整理的web-services – 使用JAXB和JAXWS Annotations将枚举属性编组到XML中全部内容,希望文章能够帮你解决web-services – 使用JAXB和JAXWS Annotations将枚举属性编组到XML中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1139594.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存