In our real projects, we often call the interfaces that others provide to us, or in our own teams,
The restful front-and-back separation also often provides a backend interface that is exposed to the app, or. net/c/c++ programmer to invoke, at this point need to use a tool or a set of programs to invoke the exposed interface.
And what I'm going to say today is one of those ways, using Apache's CXF call and exposing the interface, let's start the code directly
1, first we need to download CXF,CXF is a product of Apache, download the link attached: http://www.apache.org/dyn/closer.lua/cxf/3.2.5/apache-cxf-3.2.5.zip
Recommend that you download this
After the download is complete, the decompression is placed in a directory, you need to go to the environment variable under the path path configuration, here to see the diagram:
(This is the download of the extracted cxf, version is 3.2.5)
Environment variable configuration:
Create a new cxf_home environment variable that points to the path of your CXF after decompression
Then add this bin in path
At this point in the cmd command, enter Wsdl2java-help, you can see the output of the following content, you have configured the CXF
Open idea and we can start writing code.
2, Idea in the configuration cxf
Once open file--settings--left input cxf--select Tools under WebServices
CXF configuration is available.
3, the new project is not introduced, directly began to write an interface to expose:
Package Com.cxfdemo.hello;import javax.jws.WebService; @WebServicepublic interface MyWebService { int add (int a) int b); int minus (int a, int b);}
Write the implementation class for this interface
1 PackageCom.cxfdemo.hello;2 3 ImportJavax.jws.WebService;4 5@WebService (endpointinterface = "Com.cxfdemo.hello.MyWebService")6 Public classMywebserviceimplImplementsmywebservice{7@Override Public intAddintAintb) {8System.out.println (A + "+" +b+ "=" + (a+b));returnA +b;9 }Ten@Override Public intMinus (intAintb) { OneSystem.out.println (A + "-" + B + "=" + (A-b)); A returnAb; - } -}
This time you can write an exposed server side:
Package Com.cxfdemo.hello;import Javax.xml.ws.endpoint;public class MyServer {public static void Main (string[] args) { String address = "Http://localhost:8888/ms"; Endpoint.publish (Address,new Mywebserviceimpl ());} }
Client Test side:
1 PackageCom.cxfdemo.hello;2 3 ImportJavax.xml.namespace.QName;4 ImportJavax.xml.ws.Service;5 Importjava.net.MalformedURLException;6 ImportJava.net.URL;7 8 Public classmyclient {9 Public Static voidMain (String args[]) {Ten Try{URL url =NewURL ("http://localhost:8888/ms?wsdl"); One //Namespaces and Names AQName QName =NewQName ("http://hello.cxfdemo.com/", "Mywebserviceimplservice"); -Service service = service.create (URL, qName); MyWebService MyWebService = Service.getport (MyWebService.class); -System.out.println (Mywebservice.add (2, 3)); System.out.println (Mywebservice.minus (2, 3)); the}Catch(malformedurlexception e) { - e.printstacktrace (); - } - } +}
This time we can start the project, first run start MyServer, after the project started successfully, we can access through this link: http://localhost:8888/ms?wsdl
You can see the contents of the exposed interface at this point:
Now that the exposed interface is successful, let's start the client Test side:
To see the console output:
At this point, you can start writing a webservice-based exposure demo.
Apache CXF Exposed interface and WebService initial understanding of client invocation