Development Tip

JAXB- "값"속성이 이미 정의되어 있습니다.

yourdevel 2020. 12. 8. 20:08
반응형

JAXB- "값"속성이 이미 정의되어 있습니다. 사용하다 이 갈등을 해결하기 위해


JAXB를 사용하여 XML 바인딩 클래스 생성.

스키마는 레거시 XML 파일 세트를 기반으로하며 다음 스 니펫을 포함합니다.

<xs:complexType name="MetaType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute type="xs:string" name="Name" />
            <xs:attribute type="xs:string" name="Scheme" />
            <xs:attribute type="xs:string" name="Value" />
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

'Value'속성이의 'value'속성과 충돌 xs:string하며 다음 오류와 함께 코드 생성이 실패합니다.

com.sun.istack.SAXParseException2: Property "Value" is already defined. Use &lt;jaxb:property> to resolve this conflict. 

대답은 JAXB 바인딩 ( site-template.xjb) 을 사용하는 데 있습니다 .

<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          version="2.1">
    <bindings schemaLocation="site-template.xsd" version="1.0">
        <!-- Customise the package name -->
        <schemaBindings>
            <package name="com.example.schema"/>
        </schemaBindings>

        <!-- rename the value element -->
        <bindings node="//xs:complexType[@name='MetaType']">
            <bindings node=".//xs:attribute[@name='Value']">
                <property name="ValueAttribute"/>
            </bindings>
        </bindings>
    </bindings>
</bindings>

XPath 표현식은 노드를 찾아 이름을 변경하여 이름 충돌을 방지합니다.

이 바인딩 XML 파일을 사용하여 생성 된 Java 클래스는 원하는 getValueAttribute()(뿐만 아니라)을 갖습니다 getValue().


JAXB 바인딩 파일 생성 / 변경을 피하고 XSD 주석을 달아도 괜찮다jxb : property 주석을 속성 정의에 추가 할 수 있습니다. 예 :

<xs:complexType name="MetaType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:attribute type="xs:string" name="Name" />
            <xs:attribute type="xs:string" name="Scheme" />
            <xs:attribute type="xs:string" name="Value">
                <!-- rename property generated by JAXB (avoiding "Value" name conflict) -->
                <xs:annotation>
                    <xs:appinfo>
                        <jxb:property name="valueAttribute"/>
                    </xs:appinfo>
                </xs:annotation>
            </xs:attribute>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

xs : schema 태그에 대한 적절한 추가 :

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
           jxb:version="2.1">

아래와 같이 중복 속성 이름 "value"(중복은 JAXB에서 제공하는 기본 '값')에 대해 xxxx.xjb 파일을 생성 한 후 XJC 명령을 실행하여 JAXB 객체를 생성합니다.

xjc -p "com.track.doc"-d "C : \ JAXBDocuments \ prasam \ Desktop \ JAXB_me \ DealerTrace"appSamp.xsd -b xxxx.xjb

appSmp.xsd :-

<xsd:complexType name="range">
    <xsd:simpleContent>
        <xsd:extension base="xsd:string">
             <xsd:attribute name="value" type="xsd:string"/> 
        </xsd:extension>
    </xsd:simpleContent>        
</xsd:complexType>

xxxx.xjb :-

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns="http://java.sun.com/xml/ns/jaxb"
          xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          version="2.1">
    <bindings schemaLocation="appSmp.xsd" version="1.0">

        <schemaBindings>
            <package name="com.track.doc"/>
        </schemaBindings>    
        <bindings node="//xs:complexType[@name='range']">
            <bindings node=".//xs:attribute[@name='value']">
                <property name="valueAttribute"/>
            </bindings>
        </bindings>
    </bindings>
</bindings>

Eclipse (Helios SR1 및 Juno SR1 모두 시도) 및 CXF 2.6.3에서 솔루션을 사용하는 데 문제가 있습니다. 해결책은 Kaitsu가 말한 것과 유사했습니다. 즉, Eclipse의 New> Web Service 마법사는 wsdl을 WebContent / wsdl 폴더에 복사합니다. 나는 wsdl과 바인딩 파일을 거기에 직접 두어야했다. 그렇지 않으면 바인딩 파일에서 "is not a part of this compilation"오류가 발생했습니다.

I wasn't able to use an inline schema in the WSDL but it did work with an external schema like in answer #1.

I'm using the CXF Servlet endpoint config option. In my WSDL I have:

<wsdl:port binding="axis2:ConverterSOAP12Binding" name="ConverterSOAP12port_http">
  <soap12:address location="http://localhost/Converter/services/Converter"/>
</wsdl:port>

The wizard generated this into my web.xml, which works ok:

<servlet-mapping>
  <servlet-name>cxf</servlet-name>
  <url-pattern>/services/*</url-pattern>
</servlet-mapping>

But it put this into cxf-servlet.xml:

<jaxws:endpoint xmlns:tns="http://wtp" id="converterporttype"
implementor="wtp.ConverterPortTypeImpl" wsdlLocation="wsdl/Converter.wsdl"
endpointName="tns:ConverterSOAP12port_http" serviceName="tns:Converter"
address="/ConverterSOAP12port_http">
  <jaxws:features>
    <bean class="org.apache.cxf.feature.LoggingFeature" />
  </jaxws:features>
</jaxws:endpoint>

I had to change the address into the full URL, like this:

address="http://localhost:8080/Converter/services/Converter">

None of this bindings worked for me, i got this error:

[ERROR] La evaluación de XPath de ".//xs:attribute[@name='Value']" produce un nodo de destino vacío

It produced an empty target node... Then i realized (after 30 minutes of dispair) that my binding was aiming to a complexType instead of an element. The answer was in my xsd file.

Thank you


This bindings file mentioned in the other answer did not work for me with CXF 3.0.0. Notice that jaxb namespace has an element "bindings" and so do the namespace jaxws, so we need to declare them:

<?xml version="1.0" encoding="UTF-8"?>
<bindings xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
          xmlns="http://java.sun.com/xml/ns/jaxws"
          xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
          xmlns:xs="http://www.w3.org/2001/XMLSchema"
          wsdlLocation="mesh.wsdl" >
    <bindings node="wsdl:definitions/wsdl:types/xs:schema[...">
        <jaxb:bindings node="./xs:element[@name='Profiles']">
            <jaxb:property name="ProfilesElement"/>
        </jaxb:bindings>
    </bindings>
</bindings>

In my case the schema was already inside the WSDL so I did no have to specify the schemaLocation attribute.


you can also use the parameter -XautoNameResolution in the command line and also in the pluggin to let jxc resolve the name if you don´t bother about the name on the classes.

참고URL : https://stackoverflow.com/questions/4394134/jaxb-property-value-is-already-defined-use-jaxbproperty-to-resolve-this

반응형