简体   繁体   English

使用自定义类作为JAX-WS参数:如何为类型安全的枚举编写JAXB适配器?

[英]Using custom class as JAX-WS parameter: How to write JAXB adapter for type safe enum?

I have some old code (pre Java 1.5) that uses classes to implement type safe enums, something like: 我有一些旧代码(Java 1.5之前的版本),它们使用类来实现类型安全的枚举,例如:

public class MyTypeSafeEnum implements java.io.Serializable {
  private final int value;

  private  MyTypeSafeEnum(int value) {
   this.value = value
  }

  public static final MyTypeSafeEnum FIRST_VALUE = new MyTypeSafeEnum(0)
  public static final MyTypeSafeEnum SECOND_VALUE = new MyTypeSafeEnum(1)
  public static final MyTypeSafeEnum THIRD_VALUE = new MyTypeSafeEnum(2)

  public static fromInt(int value) {
   //Builds a MyTypeSafeEnum from the value, implementation omitted
   return theMyTypeSafeEnum;
  }

  public int getValue() {
    return this.value;
  }
 }

I want to use these enumeration classes as paremeters in a JAX-WS operation, but in the generated WSDL the definition for the type is empty: 我想将这些枚举类用作JAX-WS操作中的参数,但是在生成的WSDL中,该类型的定义为空:

<xs:complexType name="myTypeSafeEnum">
  <xs:sequence/>
</xs:complexType>

I figured I should use an XmlAdapter , so I created the following class: 我认为我应该使用XmlAdapter ,所以我创建了以下类:

public class MyTypeSafeEnumXmlAdapter extends XmlAdapter<MyTypeSafeEnum, Integer> {
  @Override
  public MyTypeSafeEnum marshal(Integer v) throws Exception {
      return MyTypeSafeEnum.fromInt(v);
  }

  @Override
  public Integer unmarshal(MyTypeSafeEnum e) throws Exception {
      return e.getValue();
  }
}

I added the @XmlJavaTypeAdapter(MyTypeSafeEnumXmlAdapter.class) annotation to my class, but it had no effect in the generated WSDL. 我在类中添加了@XmlJavaTypeAdapter(MyTypeSafeEnumXmlAdapter.class)批注,但它对生成的WSDL没有任何作用。

How I can use these classes as parameters in a JAX-WS operation? 如何在JAX-WS操作中将这些类用作参数? At the moment, refactoring the code to use the enum type is out of question. 目前,重构代码以使用enum类型enum问题。

You need to reverse the order of the types in your MyTypeSafeEnumXmlAdapter: 您需要颠倒MyTypeSafeEnumXmlAdapter中类型的顺序:

public class MyTypeSafeEnumXmlAdapter extends XmlAdapter<Integer, MyTypeSafeEnum> {
    @Override
    public Integer marshal(MyTypeSafeEnum v) throws Exception {
        return v.getValue();
    }

    @Override
    public MyTypeSafeEnum unmarshal(Integer v) throws Exception {
        return MyTypeSafeEnum.fromInt(v);
    }
}

So then if you have a class like this: 因此,如果您有这样的课程:

public class MyClass {
    private MyTypeSafeEnum someVar;

    @XmlJavaTypeAdapter(MyTypeSafeEnumXmlAdapter.class)
    public MyTypeSafeEnum getSomeVar() {
        return someVar;
    }
}

It will generate the XML like this: 它将生成如下的XML:

<xs:complexType name="myClass">
    <xs:sequence>
        <xs:element minOccurs="0" name="someVar" type="xs:int"/>
    </xs:sequence>
</xs:complexType>

The web service clients will see that field as an integer. Web服务客户端将看到该字段为整数。 So you'll have to make sure they don't pass in an integer that doesn't map to one of the enum instances, or handle it somehow. 因此,您必须确保它们不会传递不会映射到枚举实例之一的整数,或者以某种方式进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM