views:

1721

answers:

5

Hello All...

I need to jump into the Spring Web Service Project, in that I required to implement the Spring Web Service's Client Only..

So, I have already gone through with Spring's Client Reference Document.

So, I got the idea of required classes for the implementation of Client.

But my problem is like I have done some googling, but didn't get any proper example of both Client and Server from that I can implement one sample for my client.

So, if anybody gives me some link or tutorial for proper example from that I can learn my client side implementation would be greatly appreciated.

Thanks in advance...

+3  A: 

Hi Nirmal,

in my previous project, I implemented a Webservice client with Spring 2.5.6, maven2, xmlbeans.

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

I paste some codes here and hope they are helpful.

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 (either standalone or in your WSDL file, you need to extract them and save as a schema file.) under src/main/resources. when you build the project with maven, the pojos are gonna be generated by xmlbeans. The generated sourcecodes will be under target/generated-sources/xmlbeans.

then we come to Spring conf. I just put the WS relevant 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"/&gt;

        <property name="marshaller" ref="marshaller"/>

        <property name="unmarshaller" ref="marshaller"/>

    </bean>

finally, take a look the ws-client java 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 example codes are helpful.

Kent
A: 

Hi Kent, Thanks for the excellent example, i have a similar requirement but my issue is that i have to use like 9 different wsdl's as my application uses diff. methods in different wsdl's so we got the JAXB objects created and i got a .jar file created from wsdl and schema, now my job is to use that jar file invoke the different methods, can u help in this case. Really appreciate it. have posted this question on many sites but no response . PLz help [email protected]

Har
A: 

Hi nirmal....i have created a web service client in spring in my recent project....i have created the client classes and interface to fetch that operation in java desktop based application ,

if you need any help i can help u out...let me know.....

kosal