1) First of all, or package of problems, http://cxf.apache.org/download.html here can be down to the latest version of the CXF, of course, I use the latest version. Next is the nonsense, build a Web project, put in the jar package. And the jar package we don't choose, a bunch of all put in.
We'll see that it contains Spring's jar package, and then when we need to deploy CXF as a Web project, we need to use spring's configuration file, which we'll talk about later.
or an interface class and an implementation class:
Java code
- @WebService
- Public interface Ireaderservice {
- Public Reader Getreader (@WebParam (name="name") string name,@WebParam (name="password") string password);
- Public list<reader> getreaders ();
- }
Java code
- @WebService (endpointinterface="Com.cxf.servlet.IReaderService", servicename="Readerservice")
- Public class Readerservice implements ireaderservice{
- Public Reader Getreader (@WebParam (name="name") string name,@WebParam (name="password") string Password) {
- return new Reader (Name,password);
- }
- Public list<reader> getreaders () {
- list<reader> readerlist = new arraylist<reader> ();
- Readerlist.add (new Reader ("shun1","123"));
- Readerlist.add (new Reader ("shun2","123"));
- return readerlist;
- }
- }
These two classes, in addition to annotations, are the same as the webservice that were told yesterday. Here is not much to say, the explanation of the annotations, you can look at the Java EE documentation. But it should be easy to understand by meaning.
Next is JavaBean, or the reader class:
Java code
- Public class reader{
- private Static final long serialversionuid = 1L;
- private String name;
- private String password;
- Public Reader () {}
- Public Reader (String name,string password) {
- this.name = name;
- This.password = password;
- }
- //get/set Method omitted
- Public String toString () {
- return "Name:" +name+", Password:" +password;
- }
- }
The above has been finished.
2) Do we want to be a Web project? No hurry, no, CXF comes with a lightweight container service, which is equivalent to spring providing itself as an IOC container. We can use it first to test the success of our deployment.
Come directly to a test class:
Java code
- Public static void Main (string[] args) {
- System.out.println ("Server is starting ...");
- Readerservice Readerservice = new Readerservice ();
- Endpoint.publish ("Http://localhost:8080/readerService", Readerservice);
- System.out.println ("Server is started ...");
- }
It's so simple. Publish the address directly, and then specify the interface or class is OK. I use the class here, but try to use the interface, after all, interface-oriented programming is the real face of the object of thought.
We start by looking at the results:
We see that the boot is complete, and then start the browser to see if it is successful.
Directly in the browser input http://localhost:8080/readerService?wsdl, we can see:
It generates the WSDL file we need, indicating that we have successfully deployed.
3) After the successful deployment, we are going to call, its call is quite simple, similar to Xfire, get the interface, and then you can call the same method as the local class.
Java code
- Public static void Main (string[] args) {
- Jaxwsproxyfactorybean Factorybean = new Jaxwsproxyfactorybean ();
- Factorybean.setserviceclass (Ireaderservice. Class);
- Factorybean.setaddress ("Http://localhost:8080/readerService");
- Ireaderservice Readerservice = (ireaderservice) factorybean.create ();
- Reader reader = Readerservice.getreader ("Shun","123");
- System.out.println ("Reader:" +reader);
- }
Here is very simple, but also to obtain a factory class, and then directly set the interface and address and create the corresponding interface can be obtained, here and Xfire, as well as the need to call the end of the first definition of the interface prototype, otherwise these calls will not be mentioned.
We run to get the result:
No problem, consistent with the results we expected.
4) But in many cases, we don't want our webservice and our app to separate two servers, and hopefully they're in the same container, Tomcat or jboss or something else, so we'll have to deploy the webservice that we've done earlier through the web.
Note that we need to use the spring definition file here.
First look at web. xml:
Java code
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"
- xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:web="Http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- xsi:schemalocation="Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
- Id= "webapp_id" version="3.0" >
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>WEB-INF/beans.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.context.contextloaderlistener</listener-class>
- </listener>
- <servlet>
- <servlet-name>CXFServlet</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.cxfservlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>CXFServlet</servlet-name>
- <url-pattern>/webservice/*</url-pattern>
- </servlet-mapping>
- </web-app>
This is simple enough to specify the spring listener and the corresponding configuration file path, and specifies the Cxf blocking method.
Next look at Beans.xml:
Java code
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="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.xsd ">
- <import resource="Classpath:meta-inf/cxf/cxf.xml"/>
- <import resource="Classpath:meta-inf/cxf/cxf-extension-soap.xml"/>
- <import resource="Classpath:meta-inf/cxf/cxf-servlet.xml"/>
- <jaxws:endpoint id="ReaderServicce2"
- Implementor= "com.cxf.servlet.ReaderService" address="/readerservice2"/>
- </beans>
It's very simple here, just defined by Jaxws:endpoint that a webservice,implementor is the processing class of WebService, and address is its access path, similar to the Readerservice we wrote earlier.
We can then deploy it to Tomcat, which can be accessed directly through the HTTP://LOCALHOST:8080/CXFWEBSERVICE/WEBSERVICE/READERSERVICE2?WSDL.
Some friends will ask, why the URL of this visit is not the same as the previous one. In fact, the previous access address is defined by ourselves, and here the WebService address is configured in the configuration file, and is deployed through a Web project, here you need to use the project name, And we configured the Cxfservlet there Url-pattern is WebService, so the final URL is consistent with the above.
We can see the effect:
This proves our deployment was successful.
You can test it again with the previous Test class, and note that the address needs to be modified to the URL we post.
CXF compared to Xfire and more concise, although it added some annotations, but these harmless, it just put the information in the previous services.xml into the class, but more convenient maintenance, but this is a matter of opinion, some people like configuration files, and some people do not like. In addition, CXF's call Way is more concise, compared to xfire it's code volume is smaller, is a big progress.
Some friends in the process of building a number of problems, to avoid a reply, here to release code, the need for friends can download to see.
All packages in the Lib directory are not put in, and all packages of CXF are placed.
Note: The IDE is idea, the file structure is not common to eclipse, and if you need to use it under Eclipse, you can copy the code and files directly to Eclipse's new project.
"Turn" JAVA WebService CXF