Ide:
One: New MAVEN project
1.file->new->project.
2.
3.
4.
Two: Add cxf and jetty dependencies
<Dependencies> <!--Https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-frontend-jaxws - <Dependency> <groupId>Org.apache.cxf</groupId> <Artifactid>Cxf-rt-frontend-jaxws</Artifactid> <version>3.0.1</version> </Dependency> <!--Https://mvnrepository.com/artifact/org.apache.cxf/cxf-rt-transports-http-jetty - <Dependency> <groupId>Org.apache.cxf</groupId> <Artifactid>Cxf-rt-transports-http-jetty</Artifactid> <version>3.0.1</version> </Dependency></Dependencies>
Three: Add WebService service side
1.service Interface Definition:
@WebService Public Interface Service { string SayHello (string param);}
2. Interface Implementation Class
@WebService Public class Implements Service { public string SayHello (string param) { System.out.println (param+ ") Calling SayHello () method ... "); return "hello->" +param; }}
3. Service delivery
ImportOrg.apache.cxf.endpoint.Server;ImportOrg.apache.cxf.jaxws.JaxWsServerFactoryBean;/*** Description:webservice Start * WebService external exposure address is:http://localhost: 8080/SERVICE?WSDL * Created by Admin on 2017/7/26.*/ Public classserver { Public Static voidMain (string[] args) {Jaxwsserverfactorybean factory=NewJaxwsserverfactorybean (); Factory.setserviceclass (Serviceimpl.class); Factory.setaddress ("Http://localhost:8080/service"); Server server=factory.create (); Server.start (); }}
Four: Start the service side
1. Start the main method in the server
2. Browser Access http://localhost:8080/service?wsdl
WebService Service started successfully!
V: Add the client client (two ways for clients to invoke the service)
1. Static Calls
ImportCom.cxf.service.service;ImportOrg.apache.cxf.jaxws.JaxWsProxyFactoryBean;/*** Description: Static calls need to depend on the service class, * because the client calls the Cxf,webservice interface requires a server-side service, * very inconvenient, if there is no difference in the same project. * Created by Admin on 2017/7/26. */ Public classClient { Public Static voidMain (string[] args) {Jaxwsproxyfactorybean factory=NewJaxwsproxyfactorybean (); Factory.setaddress ("Http://localhost:8080/service"); Factory.setserviceclass (service.class); Service Service=(Service) factory.create (); System.out.println (Service.sayhello ("CXF")); }
2. Dynamic invocation
Importorg.apache.cxf.endpoint.Client;Importorg.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;ImportJavax.xml.namespace.QName; Public classClient {/**dynamic calls do not depend on the service class at all, * The server side can easily be called as long as the interface name and path are provided.*/ Public Static voidMain (string[] args) {jaxwsdynamicclientfactory DCF=jaxwsdynamicclientfactory.newinstance (); /**URL is the WSDL address that calls WebService*/Client Client= Dcf.createclient ("http://localhost:8080/service?wsdl"); /**namespace is the namespace (the targetnamespace property in the WSDL file), Localpart is the method name that needs to be called*/QName name=NewQName ("http://service.cxf.com/", "SayHello"); /**parameters required by the method*/String xmlstr= "CXF"; Object[] objects; Try { /**call the method and get the return value*/Objects=Client.invoke (name, XMLSTR); System.out.println (objects[0].tostring ()); } Catch(Exception e) {e.printstacktrace (); } }
3. Execute the corresponding main function in the client
Java maven CXF notes