Download the CXF package and add all of his packages into the Lib folder.
Create an interface. Add @webservice annotations
@WebService Public Interface HelloWorld { string sayhi (@WebParam (Name= "text") string text); String sayhitouser (user user); String[] Sayhitouserlist (List<User> userlist);}
Create an implementation class for the interface, add @webservice annotations, and give him a name: Boy
@WebService (servicename= "Boy") Public classHelloworldimplImplementsHelloWorld {Map<integer, user> users =NewLinkedhashmap<integer, user>(); Publicstring Sayhi (string text) {return"Hello" +text; } PublicString sayhitouser (user user) {users.put (users.size ()+ 1, user); return"Hello" +User.getname (); } PublicString[] Sayhitouserlist (list<user>userlist) {string[] result=Newstring[userlist.size ()]; inti = 0; for(User u:userlist) {Result[i]= "Hello" +U.getname (); I++; } returnresult; }}
User entity class:
Public classUser {PrivateString name; Private intAge ; PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public intGetage () {returnAge ; } Public voidSetage (intAge ) { This. Age =Age ; } }
Service-Side code:
Public class Webserviceapp { publicstaticvoid main (string[] args) { System.out.println ("Web service Start"); Helloworldimpl implementornew Helloworldimpl (); String address= "Http://localhost:8000/helloWorld"; Endpoint.publish (address, implementor); System.out.println ("Web service Started"); }}
Client code: There are integrated spring and not integrated
Public classhelloworldclient { Public Static voidMain (string[] args) {//integrating spring and CXFApplicationContext context =NewClasspathxmlapplicationcontext ("Applicationcontext.xml"); HelloWorld client1= (HelloWorld) context.getbean ("Client1"); User User1=NewUser (); User1.setname ("Tony"); User1.setage (20); User User2=NewUser (); User2.setname ("Freeman"); User2.setage (50); List<User> userlist =NewArraylist<user>(); Userlist.add (user1); Userlist.add (User2); //string[] res = client1. Sayhitouserlist (userlist); //System.out.println (res[0]); //System.out.println (res[1]);String Sayhi = Client1.sayhi ("Good"); System.out.println (Sayhi); //HelloWorld2 client2 = (HelloWorld2) context.getbean ("Client2"); //User User3 = new user (); //user3.setname ("Jerry"); //User3.setage (20); //String user = Client2.sayhitouser (user3); //System.out.println (user); //No IntegrationJaxwsproxyfactorybean SVR =NewJaxwsproxyfactorybean (); Svr.setserviceclass (HelloWorld.class); Svr.setaddress ("Http://localhost:8000/helloWorld"); HelloWorld HW=(HelloWorld) svr.create (); User User=NewUser (); User.setname ("Tony"); System.out.println (Hw.sayhi ("FFFF")); }}
The result of the printing is:
Hello Good
Hello FFFF
Configuration of Web. xml
<?XML version= "1.0" encoding= "UTF-8"?><Web-appversion= "2.5"xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <Display-name>Cxf</Display-name> <welcome-file-list> <Welcome-file>index.jsp</Welcome-file> </welcome-file-list> <Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath*:applicationcontext.xml</Param-value> </Context-param> <Listener> <Listener-class>Org.springframework.web.context.ContextLoaderListener</Listener-class> </Listener> <Listener> <Listener-class>Org.springframework.web.util.IntrospectorCleanupListener</Listener-class> </Listener> <servlet> <Servlet-name>Cxfservlet</Servlet-name> <Servlet-class>Org.apache.cxf.transport.servlet.CXFServlet</Servlet-class> <Load-on-startup>1</Load-on-startup> </servlet> <servlet-mapping> <Servlet-name>Cxfservlet</Servlet-name> <Url-pattern>/*</Url-pattern> </servlet-mapping></Web-app>
Applicationcontext.xml configuration, this file is placed in the resource files under Resources, his path in the project is in the classes inside. I have configured two sites here. It's all possible.
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:jaxws= "Http://cxf.apache.org/jaxws"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework. Org/schema/beans/spring-beans.xsd Http://cxf.apache.org/jaxws Http://cxf.apache.org/schemas/jaxws.xs D "> <!--Publish a site - <Jaxws:endpointID= "Boy"implementor= "Com.cs.webservice.HelloWorldImpl"Address= "/aaa" /> <BeanID= "Client1"class= "Com.cs.webservice.HelloWorld"Factory-bean= "ClientFactory1"Factory-method= "Create" /> <BeanID= "ClientFactory1"class= "Org.apache.cxf.jaxws.JaxWsProxyFactoryBean"> < Propertyname= "ServiceClass"value= "Com.cs.webservice.HelloWorld" /> < Propertyname= "Address"value= "Http://localhost:8000/helloWorld/aaa" /> </Bean> <!--a second site -<!--<jaxws:endpoint id= "Orange" implementor= "com.cs.webservice.HelloWorldImpl2" address= "/bbb"/> < ; Bean id= "Client2" class= "Com.cs.webservice.HelloWorld2" factory-bean= "ClientFactory2" factory-method= "create"/> ; <bean id= "ClientFactory2" class= "Org.apache.cxf.jaxws.JaxWsProxyFactoryBean" > <property name= "Serviceclas S "value=" Com.cs.webservice.HelloWorld2 "/> <property name=" Address "value=" Http://localhost:8080/cxfwebservi Cespring/bbb "/> </bean> -</Beans>
<property name= "Address" value= "http://localhost:8000/helloWorld/aaa"/>
The path before the AAA must be the same as the published string address= "Http://localhost:8000/helloWorld";
WebService Integration Spring CXF