Spring Web Client Tutorial or Example

I need to go to the Spring web service project in which I had to execute only

So, I already went through the Spring Client Reference document .

So, I got an idea of ​​the necessary classes for implementing Client.

But my problem is that I did some search queries, but did not get any suitable example of both client and server, from which I can implement one sample for my client.

So, if someone gives me some link or tutorial for the correct example from this, I can find out that my client side implementation will be very appreciated.

Thanks in advance...

+6
java spring java-ee spring-ws
source share
2 answers

In my previous project, I implemented the Webservice client with Spring 2.5.6, maven2, xmlbeans.

  • xmlbeans is responsible for un / marshal
  • maven2 is for the mgmt / building project, etc.

I am inserting some codes here and hope they will be useful.

xmlbeans maven plugin conf: (in pom.xml)

<build> <finalName>projectname</finalName> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> <resource> <directory>target/generated-classes/xmlbeans </directory> </resource> </resources> <!-- xmlbeans maven plugin for the client side --> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>xmlbeans-maven-plugin</artifactId> <version>2.3.2</version> <executions> <execution> <goals> <goal>xmlbeans</goal> </goals> </execution> </executions> <inherited>true</inherited> <configuration> <schemaDirectory>src/main/resources/</schemaDirectory> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>build-helper-maven-plugin </artifactId> <version>1.1</version> <executions> <execution> <id>add-source</id> <phase>generate-sources</phase> <goals> <goal>add-source</goal> </goals> <configuration> <sources> <source> target/generated-sources/xmlbeans</source> </sources> </configuration> </execution> </executions> </plugin> </plugins> </build> 

So, from the above conf you need to put the schema file (both stand-alone and in your WSDL file, you need to extract them and save as a schema file) in the src / main / resources section. when you create a project with maven, pojos are going to generate xmlbeans. The generated source code will be the target / source-generated / XMLBeans.

then we come to Spring conf. I just posted the WS context here:

  <bean id="messageFactory" class="org.springframework.ws.soap.axiom.AxiomSoapMessageFactory"> <property name="payloadCaching" value="true"/> </bean> <bean id="abstractClient" abstract="true"> <constructor-arg ref="messageFactory"/> </bean> <bean id="marshaller" class="org.springframework.oxm.xmlbeans.XmlBeansMarshaller"/> <bean id="myWebServiceClient" parent="abstractClient" class="class.path.MyWsClient"> <property name="defaultUri" value="http://your.webservice.url"/> <property name="marshaller" ref="marshaller"/> <property name="unmarshaller" ref="marshaller"/> </bean> 

finally look at the ws-client java class class

 public class MyWsClient extends WebServiceGatewaySupport { //if you need some Dao, Services, just @Autowired here. public MyWsClient(WebServiceMessageFactory messageFactory) { super(messageFactory); } // here is the operation defined in your wsdl public Object someOperation(Object parameter){ //instantiate the xmlbeans generated class, infact, the instance would be the document (marshaled) you are gonna send to the WS SomePojo requestDoc = SomePojo.Factory.newInstance(); // the factory and other methods are prepared by xmlbeans ResponsePojo responseDoc = (ResponsePojo)getWebServiceTemplate().marshalSendAndReceive(requestDoc); // here invoking the WS //then you can get the returned object from the responseDoc. } 

}

I hope the code examples are helpful.

+7
source share
+10
source share

All Articles