Avoid inheritance in XSD files with JAXB custom bindings.
authorgu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 9 Feb 2014 14:18:20 +0000 (15:18 +0100)
committergu.martinm@gmail.com <gu.martinm@gmail.com>
Sun, 9 Feb 2014 14:18:20 +0000 (15:18 +0100)
pom.xml
src/main/java/de/spring/webservices/business/CustomBindingBusinessExample.java [new file with mode: 0644]
src/main/java/de/spring/webservices/client/ExampleClient.java
src/main/java/de/spring/webservices/endpoints/ExampleEndPoint.java
src/main/resources/spring-configuration.xml
src/main/resources/wsdl/custombindingexample.wsdl [new file with mode: 0644]
src/main/resources/wsdl/example.wsdl [deleted file]
src/main/resources/xsd/custombinding.xjb [new file with mode: 0644]
src/main/resources/xsd/examples.xsd

diff --git a/pom.xml b/pom.xml
index 3486faa..450e486 100644 (file)
--- a/pom.xml
+++ b/pom.xml
                                 <xsdOption>
                                     <extension>true</extension>
                                     <xsd>${project.xsd.schemas.source.path}/examples.xsd</xsd>
+                                    <bindingFile>${project.xsd.schemas.source.path}/custombinding.xjb</bindingFile>
                                     <packagename>${project.xsd.schemas.package.name}</packagename>
                                     <extensionArgs>
                                         <extraarg>-Xinheritance</extraarg>
                             <sourceRoot>${project.xsd.schemas.target.path}</sourceRoot>
                             <wsdlOptions>
                                 <wsdlOption>
+                                    <!--
                                     <wsdl>${project.wsdl.sources.path}/example.wsdl</wsdl>
+                                    -->
+                                    <wsdl>${project.wsdl.sources.path}/custombindingexample.wsdl</wsdl>
                                     <extraargs>
                                         <extraarg>-xjc-Xinheritance</extraarg>
                                     </extraargs>
diff --git a/src/main/java/de/spring/webservices/business/CustomBindingBusinessExample.java b/src/main/java/de/spring/webservices/business/CustomBindingBusinessExample.java
new file mode 100644 (file)
index 0000000..864afa9
--- /dev/null
@@ -0,0 +1,24 @@
+package de.spring.webservices.business;
+
+import de.spring.webservices.auto.CustomBindingExampleRequest;
+import de.spring.webservices.auto.CustomBindingExampleResponse;
+import de.spring.webservices.auto.ObjectFactory;
+import de.spring.webservices.operations.Operations;
+
+/**
+ * Example of business class
+ *
+ */
+public class CustomBindingBusinessExample
+        implements Operations.RequestResponse<CustomBindingExampleResponse, CustomBindingExampleRequest> {
+
+    @Override
+    public CustomBindingExampleResponse requestResponse(final CustomBindingExampleRequest request) {
+        final CustomBindingExampleResponse response = new ObjectFactory().createCustomBindingExampleResponse();
+
+        response.setData("CUSTOM BINDING SNAKE EYES AND " + request.getData());
+
+        return response;
+    }
+
+}
index 6a7ccc3..3512d28 100644 (file)
@@ -1,5 +1,7 @@
 package de.spring.webservices.client;
 
+import localhost._8888.spring_ws.example.CustomBindingExampleRequest;
+import localhost._8888.spring_ws.example.CustomBindingExampleResponse;
 import localhost._8888.spring_ws.example.ExampleRequest;
 import localhost._8888.spring_ws.example.ExampleResponse;
 import localhost._8888.spring_ws.example.Examples;
@@ -30,19 +32,29 @@ public class ExampleClient {
     public void sendAndReceive() {
         final Examples exampleService = new ExamplesService().getExamplesSoap11();
         final ExampleRequest exampleRequest = new ObjectFactory().createExampleRequest();
+        final CustomBindingExampleRequest customBindingxampleRequest =
+                new ObjectFactory().createCustomBindingExampleRequest();
         exampleRequest.setData("SCARLETT. IT IS CANON.");
+        customBindingxampleRequest.setData("CUSTOM BINDING. SCARLETT. IT IS CANON.");
 
         //There are two options O.o:
 
         //1. Through Java:
         ExampleResponse exampleResponse = exampleService.example(exampleRequest);
         System.out.println(exampleResponse.getData());
+        CustomBindingExampleResponse customBindingExampleResponse =
+                exampleService.customBindingExample(customBindingxampleRequest);
+        System.out.println(customBindingExampleResponse.getData());
 
         //2. Using Spring:
         this.webServiceTemplate.setDefaultUri(this.Uri);
         exampleResponse = (ExampleResponse)
                 this.webServiceTemplate.marshalSendAndReceive(exampleRequest);
+        this.webServiceTemplate.setDefaultUri(this.Uri);
+        customBindingExampleResponse = (CustomBindingExampleResponse) this.webServiceTemplate
+                .marshalSendAndReceive(customBindingxampleRequest);
 
         System.out.println(exampleResponse.getData());
+        System.out.println(customBindingExampleResponse.getData());
     }
 }
index d423e5e..cef1f83 100644 (file)
@@ -7,6 +7,8 @@ import org.springframework.ws.server.endpoint.annotation.PayloadRoot;
 import org.springframework.ws.server.endpoint.annotation.RequestPayload;
 import org.springframework.ws.server.endpoint.annotation.ResponsePayload;
 
+import de.spring.webservices.auto.CustomBindingExampleRequest;
+import de.spring.webservices.auto.CustomBindingExampleResponse;
 import de.spring.webservices.auto.ExampleRequest;
 import de.spring.webservices.auto.ExampleResponse;
 import de.spring.webservices.operations.Operations;
@@ -16,6 +18,7 @@ import de.spring.webservices.operations.Operations;
 public class ExampleEndPoint {
     private static final String NAMESPACE_URI = "http://localhost:8888/spring-ws/example";
     private Operations.RequestResponse<ExampleResponse, ExampleRequest> example;
+    private Operations.RequestResponse<CustomBindingExampleResponse, CustomBindingExampleRequest> customBindingExample;
 
 
     public ExampleEndPoint() {}
@@ -31,10 +34,25 @@ public class ExampleEndPoint {
         return example.requestResponse(requestObject);
     }
 
+    @PayloadRoot(localPart = "CustomBindingExampleRequest", namespace = NAMESPACE_URI)
+    @ResponsePayload
+    public CustomBindingExampleResponse order(
+            @RequestPayload final CustomBindingExampleRequest requestObject,
+            @RequestPayload final Element element,
+            final MessageContext messageContext) {
+
+        return this.customBindingExample.requestResponse(requestObject);
+    }
+
 
     /** Setter required by Spring **/
     public void setExample(
             final Operations.RequestResponse<ExampleResponse, ExampleRequest> example) {
         this.example = example;
     }
+
+    public void setCustomBindingExample(
+            final Operations.RequestResponse<CustomBindingExampleResponse, CustomBindingExampleRequest> customBindingExample) {
+        this.customBindingExample = customBindingExample;
+    }
 }
index c7a3ffd..0d81fa1 100644 (file)
         <sws:annotation-driven marshaller="marshaller" unmarshaller="marshaller"/>
      -->
     <sws:annotation-driven/>
-    
+
+    <!--
+        Spring makes the WSDL file for us from the XSD file.
+        Launch the Jetty server and download WSDL file from this URL: http://127.0.0.1:8888/spring-ws/example/example.wsdl
+    -->
     <sws:dynamic-wsdl id="example"
             portTypeName="Examples"
             locationUri="/spring-ws/example">
     
     <bean id="exampleBusiness" class="de.spring.webservices.business.BusinessExample">
     </bean>
+
+    <bean id="customBindingExampleBusiness" class="de.spring.webservices.business.CustomBindingBusinessExample">
+    </bean>
     
     <bean id="exampleEndPoint" class="de.spring.webservices.endpoints.ExampleEndPoint" >
         <property name="example" ref="exampleBusiness" />  
+        <property name="customBindingExample" ref="customBindingExampleBusiness" />
     </bean>
+
     
 </beans>
diff --git a/src/main/resources/wsdl/custombindingexample.wsdl b/src/main/resources/wsdl/custombindingexample.wsdl
new file mode 100644 (file)
index 0000000..0f163b1
--- /dev/null
@@ -0,0 +1,116 @@
+<?xml version="1.0" encoding="UTF-8" standalone="no"?>
+<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch="http://localhost:8888/spring-ws/example" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost:8888/spring-ws/example" targetNamespace="http://localhost:8888/spring-ws/example">
+  <wsdl:types>
+    <xs:schema xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" jaxb:extensionBindingPrefixes="inheritance" jaxb:version="2.1" targetNamespace="http://localhost:8888/spring-ws/example">
+       
+
+       <xs:element name="ExampleRequest">
+          <xs:complexType>
+              <xs:annotation>
+                <xs:appinfo>
+                    <inheritance:implements>de.spring.webservices.operations.Request</inheritance:implements>
+                </xs:appinfo>
+            </xs:annotation>   
+            <xs:all>
+                <xs:element name="data" type="xs:string"/>
+            </xs:all>
+        </xs:complexType>
+       </xs:element>
+    <xs:element name="ExampleResponse">
+        <xs:complexType>
+        <xs:annotation>
+                <xs:appinfo>
+                    <inheritance:implements>de.spring.webservices.operations.Response</inheritance:implements>
+                </xs:appinfo>
+            </xs:annotation>  
+            <xs:all>
+                <xs:element name="data" type="xs:string"/>
+            </xs:all>
+        </xs:complexType>
+    </xs:element>
+
+    <xs:element name="CustomBindingExampleRequest">
+          <xs:complexType>
+              <xs:annotation>
+                <xs:appinfo>
+                    <inheritance:implements>de.spring.webservices.operations.Request</inheritance:implements>
+                </xs:appinfo>
+            </xs:annotation>   
+            <xs:all>
+                <xs:element name="data" type="xs:string"/>
+            </xs:all>
+        </xs:complexType>
+       </xs:element>
+    <xs:element name="CustomBindingExampleResponse">
+        <xs:complexType>
+        <xs:annotation>
+                <xs:appinfo>
+                    <inheritance:implements>de.spring.webservices.operations.Response</inheritance:implements>
+                </xs:appinfo>
+            </xs:annotation>  
+            <xs:all>
+                <xs:element name="data" type="xs:string"/>
+            </xs:all>
+        </xs:complexType>
+    </xs:element>
+
+
+</xs:schema>
+  </wsdl:types>
+  <wsdl:message name="CustomBindingExampleResponse">
+    <wsdl:part element="tns:CustomBindingExampleResponse" name="CustomBindingExampleResponse">
+    </wsdl:part>
+  </wsdl:message>
+  <wsdl:message name="CustomBindingExampleRequest">
+    <wsdl:part element="tns:CustomBindingExampleRequest" name="CustomBindingExampleRequest">
+    </wsdl:part>
+  </wsdl:message>
+  <wsdl:message name="ExampleRequest">
+    <wsdl:part element="tns:ExampleRequest" name="ExampleRequest">
+    </wsdl:part>
+  </wsdl:message>
+  <wsdl:message name="ExampleResponse">
+    <wsdl:part element="tns:ExampleResponse" name="ExampleResponse">
+    </wsdl:part>
+  </wsdl:message>
+  <wsdl:portType name="Examples">
+    <wsdl:operation name="CustomBindingExample">
+      <wsdl:input message="tns:CustomBindingExampleRequest" name="CustomBindingExampleRequest">
+    </wsdl:input>
+      <wsdl:output message="tns:CustomBindingExampleResponse" name="CustomBindingExampleResponse">
+    </wsdl:output>
+    </wsdl:operation>
+    <wsdl:operation name="Example">
+      <wsdl:input message="tns:ExampleRequest" name="ExampleRequest">
+    </wsdl:input>
+      <wsdl:output message="tns:ExampleResponse" name="ExampleResponse">
+    </wsdl:output>
+    </wsdl:operation>
+  </wsdl:portType>
+  <wsdl:binding name="ExamplesSoap11" type="tns:Examples">
+    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
+    <wsdl:operation name="CustomBindingExample">
+      <soap:operation soapAction=""/>
+      <wsdl:input name="CustomBindingExampleRequest">
+        <soap:body use="literal"/>
+      </wsdl:input>
+      <wsdl:output name="CustomBindingExampleResponse">
+        <soap:body use="literal"/>
+      </wsdl:output>
+    </wsdl:operation>
+    <wsdl:operation name="Example">
+      <soap:operation soapAction=""/>
+      <wsdl:input name="ExampleRequest">
+        <soap:body use="literal"/>
+      </wsdl:input>
+      <wsdl:output name="ExampleResponse">
+        <soap:body use="literal"/>
+      </wsdl:output>
+    </wsdl:operation>
+  </wsdl:binding>
+  <wsdl:service name="ExamplesService">
+    <wsdl:port binding="tns:ExamplesSoap11" name="ExamplesSoap11">
+      <soap:address location="http://127.0.0.1:8888/spring-ws/example"/>
+    </wsdl:port>
+  </wsdl:service>
+</wsdl:definitions>
\ No newline at end of file
diff --git a/src/main/resources/wsdl/example.wsdl b/src/main/resources/wsdl/example.wsdl
deleted file mode 100644 (file)
index 8a75ff5..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<wsdl:definitions xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:sch="http://localhost:8888/spring-ws/example" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://localhost:8888/spring-ws/example" targetNamespace="http://localhost:8888/spring-ws/example">
-  <wsdl:types>
-    <xs:schema xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" jaxb:extensionBindingPrefixes="inheritance" jaxb:version="2.1" targetNamespace="http://localhost:8888/spring-ws/example">
-       
-
-       <xs:element name="ExampleRequest">
-          <xs:complexType>
-              <xs:annotation>
-                <xs:appinfo>
-                    <inheritance:implements>de.spring.webservices.operations.Request</inheritance:implements>
-                </xs:appinfo>
-            </xs:annotation>   
-            <xs:all>
-                <xs:element name="data" type="xs:string"/>
-            </xs:all>
-        </xs:complexType>
-       </xs:element>
-    <xs:element name="ExampleResponse">
-        <xs:complexType>
-        <xs:annotation>
-                <xs:appinfo>
-                    <inheritance:implements>de.spring.webservices.operations.Response</inheritance:implements>
-                </xs:appinfo>
-            </xs:annotation>  
-            <xs:all>
-                <xs:element name="data" type="xs:string"/>
-            </xs:all>
-        </xs:complexType>
-    </xs:element>
-</xs:schema>
-  </wsdl:types>
-  <wsdl:message name="ExampleRequest">
-    <wsdl:part element="tns:ExampleRequest" name="ExampleRequest">
-    </wsdl:part>
-  </wsdl:message>
-  <wsdl:message name="ExampleResponse">
-    <wsdl:part element="tns:ExampleResponse" name="ExampleResponse">
-    </wsdl:part>
-  </wsdl:message>
-  <wsdl:portType name="Examples">
-    <wsdl:operation name="Example">
-      <wsdl:input message="tns:ExampleRequest" name="ExampleRequest">
-    </wsdl:input>
-      <wsdl:output message="tns:ExampleResponse" name="ExampleResponse">
-    </wsdl:output>
-    </wsdl:operation>
-  </wsdl:portType>
-  <wsdl:binding name="ExamplesSoap11" type="tns:Examples">
-    <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
-    <wsdl:operation name="Example">
-      <soap:operation soapAction=""/>
-      <wsdl:input name="ExampleRequest">
-        <soap:body use="literal"/>
-      </wsdl:input>
-      <wsdl:output name="ExampleResponse">
-        <soap:body use="literal"/>
-      </wsdl:output>
-    </wsdl:operation>
-  </wsdl:binding>
-  <wsdl:service name="ExamplesService">
-    <wsdl:port binding="tns:ExamplesSoap11" name="ExamplesSoap11">
-      <soap:address location="http://localhost:8888/spring-ws/example"/>
-    </wsdl:port>
-  </wsdl:service>
-</wsdl:definitions>
\ No newline at end of file
diff --git a/src/main/resources/xsd/custombinding.xjb b/src/main/resources/xsd/custombinding.xjb
new file mode 100644 (file)
index 0000000..9ceb3a9
--- /dev/null
@@ -0,0 +1,36 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<jaxb:bindings xmlns:xsd="http://www.w3.org/2001/XMLSchema"
+    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
+    xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
+    jaxb:version="2.1"
+    jaxb:extensionBindingPrefixes="inheritance">
+    
+    <!--
+    Se procesa con Xpath si quieres meter expresiones regulares y cosas así en teoría dependes de Xpath
+    por ejemplo esto: @name=match[.] se supone que debería funcionar pero me dice que no puede
+    cambiar mas de un nodo a la vez. Puede que sea un bug de xjc que se carga las opciones de Xpath :/
+    <jaxb:bindings schemaLocation="examples.xsd"> 
+        <jaxb:bindings node="//xsd:element[@name='ExampleRequest']/xsd:complexType">
+                <inheritance:implements>de.spring.webservices.operations.Request</inheritance:implements>
+        </jaxb:bindings>
+        <jaxb:bindings node="//xsd:element[@name='ExampleResponse']/xsd:complexType">
+                <inheritance:implements>de.spring.webservices.operations.Response</inheritance:implements>
+        </jaxb:bindings>
+    </jaxb:bindings>
+    -->
+
+    <jaxb:bindings schemaLocation="examples.xsd"> 
+        <jaxb:bindings node="//xsd:element[@name='CustomBindingExampleRequest']/xsd:complexType">
+                <inheritance:implements>de.spring.webservices.operations.Request</inheritance:implements>
+        </jaxb:bindings>
+    </jaxb:bindings>
+
+
+    <jaxb:bindings schemaLocation="examples.xsd"> 
+        <jaxb:bindings node="//xsd:element[@name='CustomBindingExampleResponse']/xsd:complexType">
+                <inheritance:implements>de.spring.webservices.operations.Response</inheritance:implements>
+        </jaxb:bindings>
+    </jaxb:bindings>
+
+</jaxb:bindings>
index 897d024..d951278 100644 (file)
             </xs:all>
         </xs:complexType>
     </xs:element>
+
+    <xs:element name="CustomBindingExampleRequest">
+          <xs:complexType>
+            <xs:all>
+                <xs:element name="data" type="xs:string" />
+            </xs:all>
+        </xs:complexType>
+       </xs:element>
+    <xs:element name="CustomBindingExampleResponse">
+        <xs:complexType>
+            <xs:all>
+                <xs:element name="data" type="xs:string" />
+            </xs:all>
+        </xs:complexType>
+    </xs:element>
+
+
 </xs:schema>