jaxb2: web-services-spring-jaxb2-client using @Service annotation and creating schema...
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sat, 19 Dec 2015 23:35:48 +0000 (00:35 +0100)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sat, 19 Dec 2015 23:35:48 +0000 (00:35 +0100)
jaxb2/web-services-spring-jaxb2-client/src/main/java/de/spring/webservices/client/ExampleClientService.java
jaxb2/web-services-spring-jaxb2-client/src/main/resources/examples.xsd [deleted file]
jaxb2/web-services-spring-jaxb2-client/src/main/resources/schemas/examples.xsd [new file with mode: 0644]
jaxb2/web-services-spring-jaxb2-client/src/main/resources/spring-configuration/ws/client-spring-configuration.xml

index df603df..309a461 100644 (file)
@@ -1,6 +1,7 @@
 package de.spring.webservices.client;
 
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Service;
 import org.springframework.ws.client.core.WebServiceTemplate;
 
 import de.spring.webservices.auto.CustomBindingExampleRequest;
@@ -16,6 +17,7 @@ import de.spring.webservices.auto.ExampleResponse;
  * information from our Web Services.
  * 
  */
+@Service
 public class ExampleClientService {    
     private final WebServiceTemplate webServiceTemplate;
 
diff --git a/jaxb2/web-services-spring-jaxb2-client/src/main/resources/examples.xsd b/jaxb2/web-services-spring-jaxb2-client/src/main/resources/examples.xsd
deleted file mode 100644 (file)
index 96c3737..0000000
+++ /dev/null
@@ -1,92 +0,0 @@
-<?xml version="1.0" encoding="UTF-8" ?>
-<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
-    xmlns:parent="http://gumartinm.name/spring-ws/parent"
-    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
-    xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
-    xmlns:annox="http://annox.dev.java.net"
-    xmlns:example="http://gumartinm.name/spring-ws/example"
-    jaxb:version="2.1"
-    jaxb:extensionBindingPrefixes="inheritance annox"
-    elementFormDefault="qualified"
-    targetNamespace="http://gumartinm.name/spring-ws/example">
-
-    <!--
-        We are going to use catalog.cat in order to avoid downloading parent.xsd from remote server
-    -->
-    <xs:import namespace="http://gumartinm.name/spring-ws/parent" schemaLocation="http://gumartinm.name/spring-ws/parent/parent.xsd" />
-
-
-       <!-- Using inheritance and annox plugin -->
-       <xs:element name="ExampleRequest">
-          <xs:complexType>
-              <xs:annotation>
-                <xs:appinfo>
-                    <annox:annotate>@java.lang.SuppressWarnings({"unchecked","rawtypes"})</annox:annotate> 
-                    <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>
-
-
-       <!-- Using custombinding.xjb instead of inheritance plugin.
-                Useful when you can not modify your xsd files because they are provided
-                by another person or company
-       -->
-    <xs:element name="CustomBindingExampleRequest">
-          <xs:complexType>
-            <xs:all>
-                <xs:element name="data" type="xs:string" />
-                <xs:element name="exampleDate" type="xs:dateTime" minOccurs="0" maxOccurs="1" />
-                <xs:element name="parentEnum" type="parent:parentEnumType" minOccurs="0" maxOccurs="1" />
-            </xs:all>
-        </xs:complexType>
-       </xs:element>
-    <xs:element name="CustomBindingExampleResponse">
-        <xs:complexType>
-            <xs:all>
-                <xs:element name="data" type="xs:string" />
-                <xs:element name="exampleDate" type="xs:dateTime" minOccurs="0" maxOccurs="1" />
-                <xs:element name="parentEnum" type="parent:parentEnumType" minOccurs="0" maxOccurs="1" />
-            </xs:all>
-        </xs:complexType>
-    </xs:element>
-
-       <!-- Example of creating array list by means of XSD -->
-    <xs:complexType name="car">
-       <xs:sequence>
-               <xs:element name="data" type="xs:string" />
-               <xs:element name="parentEnum" type="parent:parentEnumType" minOccurs="0" maxOccurs="100" />
-       </xs:sequence>
-    </xs:complexType>
-    <xs:complexType name="truck">
-       <xs:all>
-               <xs:element name="data" type="xs:string" />
-               <xs:element name="parentEnum" type="parent:parentEnumType" minOccurs="0" maxOccurs="1" />
-       </xs:all>
-    </xs:complexType>
-    <xs:element name="vehicles">
-        <xs:complexType>
-               <xs:sequence>
-                               <xs:element name="cars" type="example:car" maxOccurs="unbounded" />
-                               <xs:element name="trucks" type="example:truck" minOccurs="0" maxOccurs="100" />
-            </xs:sequence>
-        </xs:complexType>
-    </xs:element>
-
-</xs:schema>
diff --git a/jaxb2/web-services-spring-jaxb2-client/src/main/resources/schemas/examples.xsd b/jaxb2/web-services-spring-jaxb2-client/src/main/resources/schemas/examples.xsd
new file mode 100644 (file)
index 0000000..e73313a
--- /dev/null
@@ -0,0 +1,128 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
+    xmlns:parent="http://gumartinm.name/spring-ws/parent"
+    xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
+    xmlns:inheritance="http://jaxb2-commons.dev.java.net/basic/inheritance"
+    xmlns:annox="http://annox.dev.java.net"
+    xmlns:example="http://gumartinm.name/spring-ws/example"
+    jaxb:version="2.1"
+    jaxb:extensionBindingPrefixes="inheritance annox"
+    elementFormDefault="qualified"
+    targetNamespace="http://gumartinm.name/spring-ws/example">
+    
+    <!--
+         When should an item be declared as an element versus when should it be defined as a type?
+         When in doubt, make it a type. You can always create an element from the type, if needed. With a type, other elements can reuse that type.
+         
+         <xs:all> specifies that the child elements can appear in any order.
+         
+         <xs:sequence> specifies child elements can only appear in the order mentioned.
+    -->
+
+    <!--
+        We are going to use catalog.cat in order to avoid downloading parent.xsd from remote server
+        when creating Java objects from examples.xsd.
+    -->
+    <xs:import namespace="http://gumartinm.name/spring-ws/parent" schemaLocation="http://gumartinm.name/spring-ws/parent/parent.xsd" />
+
+
+       <!-- Spring requires the following:
+            1. XSD elements being used as request must end with Request name.
+            2. XSD elements being used as response must end with Response name.
+
+                IN THIS WAY SPRING FINDS OUT HOW TO CREATE THE wsdl:operation IN THE AUTOGENERATED WSDL.
+                
+                ExampleRequest and ExampleResponse will be associated to the wsdl:operation Example in the autogenerated wsdl and
+                the wsdl:operation Example will have the wsdl:request ExampleRequest and wsdl:response ExampleResponse  elements.
+                The same for CustomBindingExample.
+       -->
+       <!-- Using inheritance and annox plugin -->
+       <xs:element name="ExampleRequest">
+          <xs:complexType>
+              <xs:annotation>
+                <xs:appinfo>
+                    <annox:annotate>@java.lang.SuppressWarnings({"unchecked","rawtypes"})</annox:annotate> 
+                    <inheritance:implements>de.spring.webservices.operations.Request</inheritance:implements>
+                </xs:appinfo>
+            </xs:annotation>   
+            <xs:all>
+                <xs:element name="data" type="parent:limitedString" />
+            </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="ExampleFault">
+       <xs:complexType>
+               <xs:sequence>
+                       <xs:element name="technicalError" type="xs:string" />
+                       <xs:element name="elements" type="parent:element" minOccurs="0" maxOccurs="unbounded"/>
+               </xs:sequence>
+        </xs:complexType>
+    </xs:element>
+
+
+       <!-- Using custombinding.xjb instead of inheritance plugin.
+                Useful when you can not modify your xsd files because they are provided
+                by another person or company
+       -->
+    <xs:element name="CustomBindingExampleRequest">
+          <xs:complexType>
+            <xs:all>
+                <xs:element name="data" type="xs:string" />
+                <xs:element name="exampleDate" type="xs:dateTime" minOccurs="0" maxOccurs="1" />
+                <xs:element name="parentEnum" type="parent:parentEnumType" minOccurs="0" maxOccurs="1" />
+            </xs:all>
+        </xs:complexType>
+       </xs:element>
+    <xs:element name="CustomBindingExampleResponse">
+        <xs:complexType>
+            <xs:all>
+                <xs:element name="data" type="xs:string" />
+                <xs:element name="exampleDate" type="xs:dateTime" minOccurs="0" maxOccurs="1" />
+                <xs:element name="parentEnum" type="parent:parentEnumType" minOccurs="0" maxOccurs="1" />
+            </xs:all>
+        </xs:complexType>
+    </xs:element>
+    <xs:element name="CustomBindingExampleFault">
+       <xs:complexType>
+               <xs:sequence>
+                       <xs:element name="technicalError" type="xs:string" />
+                       <xs:element name="elements" type="parent:element" minOccurs="0" maxOccurs="unbounded"/>
+               </xs:sequence>
+        </xs:complexType>
+    </xs:element>
+
+       <!-- Example of creating array list by means of XSD -->
+    <xs:complexType name="car">
+       <xs:sequence>
+               <xs:element name="data" type="xs:string" />
+               <xs:element name="parentEnum" type="parent:parentEnumType" minOccurs="0" maxOccurs="100" />
+       </xs:sequence>
+    </xs:complexType>
+    <xs:complexType name="truck">
+       <xs:all>
+               <xs:element name="data" type="xs:string" />
+               <xs:element name="parentEnum" type="parent:parentEnumType" minOccurs="0" maxOccurs="1" />
+       </xs:all>
+    </xs:complexType>
+    <xs:element name="vehicles">
+        <xs:complexType>
+               <xs:sequence>
+                               <xs:element name="cars" type="example:car" maxOccurs="unbounded" />
+                               <xs:element name="trucks" type="example:truck" minOccurs="0" maxOccurs="100" />
+            </xs:sequence>
+        </xs:complexType>
+    </xs:element>
+
+</xs:schema>
index 785d2db..7d43bae 100644 (file)
         receive data from the Web Services.
     -->
    
-    <!-- Searches for @Endpoint  --> 
+    <!-- Searches for beans in packages (instead of XML configuration we can use in this way annotations like @Service, @Component, etc, etc)  --> 
     <context:component-scan base-package="de.spring.webservices"/>
 
-    <oxm:jaxb2-marshaller id="marshaller" context-path="de.spring.webservices.auto"/>
-       
-    <!-- Searches for @PayloadRoot --> 
-    <sws:annotation-driven marshaller="marshaller" unmarshaller="marshaller"/>
+    <!--
+        Three ways of using a marshallers/unmarshallers.
+        
+        1. No declarar nada en el XML y dejar que Spring lo haga internamente todo por nosotros.
+        Esto equivale a esta configuracion en XML
+        
+        <oxm:jaxb2-marshaller id="marshaller" context-path="de.spring.webservices"/>
+        El context-path Spring supongo que lo rellena automáticamente en base al component-scan declarado arriba.
+        
+        2. Especificando el context-path para ser escaneado por Spring usando anotaciones. Esto
+        se hace de este modo:
+        
+        <oxm:jaxb2-marshaller id="marshaller" context-path="de.spring.webservices.auto"/>
+        Esto es lo mismo que haría Spring si no declaramos nada en el XML pero así tenemos opción de
+        de especificar un context-path en concreto.
+        
+        3. Especificando la implementación concreta del marshaller.
+        Con esta opción además puedo usar packagesToScan, contest-path si no recuerdo mal tenía problemas
+        cuando había dos ObjectFactory con el mismo package. Uno está en globalxsds y otro en este proyecto.
+        De todos modos, probablemente habría que usar un package distinto para lo que hay
+       en globalxsds (quizás incluso basado en el namespace del xsd) y así podría evitar esta configuración. 
+     -->
+       <bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
+       <property name="packagesToScan" value="de.spring.webservices.auto"/>
+       </bean>
      
    
     <!-- Required in order to use SOAP 1.2
         </property>
     </bean> 
     
-    
-    <sws:interceptors>
-        <bean class="org.springframework.ws.soap.server.endpoint.interceptor.SoapEnvelopeLoggingInterceptor">
-            <property name="logRequest" value="true"/>
-            <property name="logResponse" value="true"/>
-        </bean>
-    </sws:interceptors>
 
     <!-- 
         ¿Este validador funciona teniendo inheritance en el xsd? (inheritances es una cosa especial 
@@ -62,8 +76,8 @@
         class="org.springframework.ws.client.support.interceptor.PayloadValidatingInterceptor">
         <property name="schemas">
             <list>
-                <value>classpath:examples.xsd</value>
-                <value>classpath:parent.xsd</value>
+                <value>classpath:schemas/examples.xsd</value>
+                <value>classpath:schemas/parent.xsd</value>
             </list>
         </property>
         <property name="validateRequest" value="true"/>
         </property>
     </bean>
     
+    <!--
+    Using @Service and @Autowired
+    We could use just XML configuration, or XML confirguration and @Autowired or as I am doing now @Service and @Autowired.
     <bean id="exampleClient" class="de.spring.webservices.client.ExampleClientService">
-        <!--
+        
         @Autowired works even using XML configuration as long as you use context:component-scan
         <property name="webServiceTemplate" ref="webServiceTemplate"/>
-        -->
     </bean>
+    -->
     
 </beans>