Routing lists attributes in XML using JAXB and JAXWS Annotations

Let's say we have the following listing of 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; } } 

I am using Apache CXF, and the generated WSDL, as expected, translates the above enumeration into a constraint:

 <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> 

So far, so good, and this is a desirable feature. I myself remember to struggle with such structures before Apache CXF (back when I used XFire).

However, this is not the case here. I want to produce a different result. I want the enumeration to be converted to a complex type and that both code and message attributes are translated into XML elements when the object containing an instance of this enumeration is sorted. I just want it to not behave like a rename. I know I could do this if I used a simple class instead of an enumeration. However, I would very much like to keep it by listing, so I saved it as text in the java part of the code.

If the generated WSDL could still have a restriction on the possible values, this would be an ideal scenario. However, I could do without him. The main thing here would be to save it as a renaming of Java 1.5, while at the same time sorting (and generating WSDL) ReturnCode as a complex type with code and a message as its elements.

I tried to hint that with JAXWS Annotations data placed in the source code of the enumeration. Is there any way to do this with these (or some other) annotations? Or do I need to write my own marshaller / unmarshaller and WSDL generator?

Many thanks!

Yours faithfully,

Filipe Fedalto

+7
source share
1 answer

Use the enumerations in your Java server code and translate them into a complex type in the interface of your service.

Example:

 @WebMethod public ComplexType GetInfo(){ ReturnCode response; response = ReturnCode.OK; ComplexType wsResponse; wsResponse = response.toComplexType() return wsResponse; } @WebMethod public void PutInfo(ComplexType input){ ReturnCode request = ReturnCode.fromComplexType(input); //more code } 
0
source

All Articles