Does CXF not generate enumeration mapping?

I am generating classes with CXF (wsdl2java) from wsdl files, but one enumeration is displayed only in String instead.

If I open the generated class, this is a wsdl fragment:

  <complexType> <complexContent> <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> <attribute name="Type" use="required"> <simpleType> <restriction base="{http://www.w3.org/2001/XMLSchema}string"> <enumeration value="AAA"/> <enumeration value="VVV"/> </restriction> </simpleType> </attribute> </restriction> </complexContent> </complexType> 

Why is the result a String and not Enum ? This is the automatically generated result:

 private String type; public String getType() { return type; } public void setType(String value) { this.type = value; } 

Update: bind file:

 <jaxb:bindings xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" jaxb:version="2.1"> <jaxb:bindings> <jaxb:bindings node="//xs:attribute[@name='Type']/xs:simpleType"> <jaxb:typesafeEnumClass ref="TestEnum" /> </jaxb:bindings> </jaxb:bindings> </jaxb:bindings> 
+1
source share
2 answers

As a result of my experiments, the following information appeared. I could not find anything useful in the CXF manuals.

  • if you declare a type inside another type, and this is simpletype , you do not have type NO in the container java class, but only in the field. If your restriction was based on string , you will have a field of type string .
  • if this inner type is complextype (you should put a simplecontent element between complextype and restriction ), you have an inner class with the correct name, but it is NOT a real enumeration. You can get the String value on getValue() . You can use any string data for it and not get errors. (IMHO, an absolutely useless option)
  • if you declare your enumeration as complextype without a container type, you will have it as an open non-inner class. Otherwise, he is like the previous one. Again, this is not an enumeration, not a validation, no real limitations. Useless.
  • if you declare your enumeration type outside of any container type, and it will be simpletype , you will have a public non-internal enumeration. Obviously, this is what you would like to see.

To make matters worse, even the fourth option will not catch the error in the XML message for you. If:

 enumeration StyleType {A,B,C} ... StyleType Style 

And you have an XML message with the wrong (not one of the values โ€‹โ€‹A, B, C) for Style, you just get null when using getStyle() . So, instead of having a nice message "in ... a message on the line ... position ... there is incorrect data", you should add a check for non-null after each gerStyle (). If you do not want the user to receive a NullPointerException .

+1
source

"... one enumeration is instead displayed only in String."

"Why do we need String, not Enum?"

This can happen if one of your enumeration values โ€‹โ€‹is not a valid Java identifier (for example: starts with a digit) - JAXB (which cxf wsdl2java delegates work wsdl2java ) de facto forcibly uses the String field, otherwise the generated code will not compile.

Recommended reading: http://blog.bdoughan.com/2011/08/jaxb-and-enums.html

0
source

Source: https://habr.com/ru/post/1483388/


All Articles