Two development methods of WebService client when xfire is integrated with spring

Source: Internet
Author: User

Method 1,Create a WebService client using spring and the WSDL File

Method 2,Create a WebService client using spring and the WSDL access address

The client code is provided here, and the corresponding server code
See my previous blog post --The second method of xfire integration with spring

Method 1. Create a WebService client using spring and the WSDL File

Package COM. jadyer. client; <br/> Import Java. io. ioexception; <br/> Import Org. codehaus. xfire. client. client; <br/> Import Org. springframework. core. io. classpathresource; <br/> Import Org. springframework. core. io. resource; </P> <p>/** <br/> * copy the WSDL file provided by the service provider to the src directory, you can <br/> */<br/> public class clientusespringfromwsdl {<br/> Public static void main (string [] ARGs) throws ioexception, exception {<br/> New clientusespringfromwsdl (). generatedclient (); <br/>}</P> <p> Public void generatedclient () throws ioexception, exception {<br/> // copy the corresponding WSDL file to the src directory <br/> string WSDL = "helloservice. WSDL "; <br/> // load the WSDL file <br/> resource = new classpathresource (WSDL ); <br/> // create a client instance based on WSDL <br/> Client client = new client (resource. getinputstream (), null); <br/> // call a specific web service method <br/> object [] result = client. invoke ("sayhello", new object [] {""}); <br/> // output the returned results of the server method <br/> system. out. println (result [0]); <br/>}< br/>}

Method 2: Create a WebService client using spring and the WSDL access address

Package COM. jadyer. client; <br/> Import Java. util. list; <br/> Import Org. springframework. context. applicationcontext; <br/> Import Org. springframework. context. support. classpathxmlapplicationcontext; <br/> Import COM. jadyer. model. person; <br/> Import COM. jadyer. model. user; <br/> Import COM. jadyer. server. helloservice; </P> <p>/** <br/> * at this time, the service provider should provide us with two things: <br/> * Create a client in the src directory. XML file used by the client to call <br/> */<br/> public class clientuserspringfromxml {<br/> Public static void main (string [] ARGs) {<br/> New clientuserspringfromxml (). generatedclient (); <br/>}</P> <p> Public void generatedclient () {<br/> applicationcontext CTX = new classpathxmlapplicationcontext ("client. XML "); <br/> helloservice = (helloservice) CTX. getbean ("xfireserverdemo"); </P> <p> // test the sayhello () method <br/> system. out. println (helloservice. sayhello (""); </P> <p> // test the getperson () method <br/> User UU = new user (); <br/> UU. setname ("Yang Guo"); <br/> person pp = helloservice. getperson (UU); <br/> system. out. println (pp. getname (); </P> <p> // test the getpersonlist () method <br/> List <person> personlist = helloservice. getpersonlist (24, ""); <br/> for (person P: personlist) {<br/> system. out. println (P. getname (); <br/>}< br/>}

Next we will use the client. xml file.

<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <! Doctype beans public "-// spring // DTD bean // en" "http://www.springframework.org/dtd/spring-beans.dtd"> <br/> <beans> <br/> <bean id = "xfireserverdemo" class = "org. codehaus. xfire. spring. remoting. xfireclientfactorybean "> <br/> <property name =" serviceclass "> <br/> <value> COM. jadyer. server. helloservice </value> <br/> </property> <br/> <property name = "wsdldocumenturl"> <br/> <value> http: // 127.0.0.1: 8080/xfire_spri Ng_03/services/helloservice? WSDL </value> <br/> </property> <br/> </bean> <br/> </beans> <br/> <! -- <Br/> This is a spring configuration file called by the client. An xfireserverdemo bean is defined here. <br/> the bean accesses wsdldocumenturl as http: // 127.0.0.1: 8080/xfire_spring_02/xfireserver. WS? WSDL <br/> -->

Finally, the interfaces provided by the service provider and the VO classes used by the interfaces

Package COM. jadyer. server; <br/> Import Java. util. list; <br/> Import COM. jadyer. model. person; <br/> Import COM. jadyer. model. user; </P> <p>/** <br/> * exposes a Web Service Interface Class <br/> */<br/> Public interface helloservice {<br/> public String sayhello (string name ); // transfer a simple object <br/> Public Person getperson (User U); // transfer an object <br/> public list <person> getpersonlist (integer age, string name); // transfer list <br/>}</P> <p> package COM. jadyer. model; <br/>/** <br/> * vo class used by the interface <br/> */<br/> public class user {<br/> private string name; <br/>/* -- getter and setter are omitted -- */<br/>}</P> <p> package COM. jadyer. model; <br/>/** <br/> * vo class used by the interface <br/> */<br/> public class person {<br/> private integer age; <br/> private string name; <br/>/* -- getter and setter -- */<br/>}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.