Describes in detail the steps and methods for creating, publishing, and accessing the cxf service in the Tomcat container.
I. Server Side
1. Add a cxf package
1) Basic Package:
Commons-logging-1.1.1.jar
Geronimo-activation_1.1_spec-1.0.2.jar
Geronimo-annotation_1.0_spec-1.1.1.jar
Geronimo-javamail_1.4_spec-1.6.jar
Geronimo-jaxws_2.1_spec-1.0.jar
Geronimo-servlet_2.5_spec-1.2.jar
Geronimo-stax-api_1.0_spec-1.0.1.jar
Geronimo-ws-metadata_2.0_spec-1.1.2.jar
Jaxb-api-2.1.jar
Jaxb-impl-2.1.12.jar
Jetty-6.1.21.jar
Jetty-util-6.1.21.jar
Neethi-2.0.4.jar
Saaj-api-1.3.jar
Saaj-impl-1.3.2.jar
Wsdl4j-1.6.2.jar
Wstx-asl-3.2.8.jar
Xml-resolver-1.2.jar
XmlSchema-1.4.5.jar
2) JSF and jstl)
Jsf-api.jar
Jsf-impl.jar
Jstl-1.2.jar
3) cxf
Cxf-2.2.4.jar
4) add spring support for xml configuration in spring jars.
Aopalliance-1.0.jar
Spring-core-2.5.5.jar
Spring-beans-2.5.5.jar
Spring-context-2.5.5.jar
Spring-web-2.5.5.jar
2. service interfaces and implementation
1) interface class, such as helloworld. Java:
PackageCxf. test;
ImportJavax. JWS. WebService;
@ WebService
Public InterfaceHelloworld
{
// A simple method that returns a string
String say (string Hello );
}
2) implementation class, such as helloworldimpl. Java:
PackageCxf. test;
ImportJavax. JWS. WebService;
// WebService implementation class.
// Use @ WebService to point to the interface definition class.
@ WebService (endpointinterface = "cxf. Test. helloworld ")
Public ClassHelloworldimplImplementsHelloworld
{
PublicString say (string Hello)
{
Return"Hello" + hello;
}
}
3. service configuration
1) web. xml
<? XML version = "1.0" encoding = "UTF-8"?>
<Web-app version = "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>
<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>/services/* </url-pattern>
</Servlet-mapping>
</Web-app>
2) Beans. xml
<? 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. xml"/>
<Import resource = "classpath: META-INF/cxf/cxf-extension-soap.xml"/>
<Import resource = "classpath: META-INF/cxf/cxf-servlet.xml"/>
<Jaxws: endpoint id = "webservicehelloworld"
Address = "/helloworld"
Implementor = "cxf. Test. helloworldimpl"/>
</Beans>
Note: The service does not require configuration such as index. jsp. myeclipse can be deleted automatically, without any impact on the service and configuration.
4. Publish the Service (to Tomcat)
Method 1: release through myeclipse
Click the toolbar icon, or right-click Project> myeclipse> add and remove project deployments.
Method 2: Use ant.
Ii. Client
Create a web project and follow these steps. You do not need to set web. xml.
1. Add a cxf package
Different methods require different packages. As shown in method 3 of "3. service call" below, the following package is required:
Commons-logging-1.1.1.jar
Cxf-2.2.4.jar
Neethi-2.0.4.jar
Spring-core-2.5.5.jar
Spring-beans-2.5.5.jar
Spring-context-2.5.5.jar
Wsdl4j-1.6.2.jar
XmlSchema-1.4.5.jar
2. Obtain the Service Interface Class (similar to the. h header file in C/C ++)
Method 1: Copy data directly from the original project
This is of course the simplest method and the most "difficult" method (if the service is not self-built, it is obviously not accessible ).
Method 2: generate from the WSDL document.
Install the cxf package first. The generation process is as follows:
1) install cxf and set the environment variables, such as: D: \ apache \ apache-cxf-2.2.4; and add '; % cxf_home % \ bin' After path (optional ). The usage of wsdl2java is as follows:
Wsdl2java-P package name-d directory name WSDL path
For example, wsdl2java-P demo. Service. Client-d e: \ SRC htt: // localhost: 8080/helloworld? WSDL
-P specifies the namespace of the WSDL, that is, the package name of the Code to be generated.
-D specifies the directory where the code to be generated is located
-The client generates the code for the client to test the web service.
-The server generates the code for the server to start the web service.
-Impl: generate the Web service implementation code
-Ant generate the build. xml file
-Compile the code generated by compile
-Quient silent mode. No warning or error messages are output.
-All: generate all the starting endpoint codes: types, Service proxy, service interface, server mainline, client mainline, implementation object, and an ant build. xml file.
2) run the wsdl2java batch processing program, for example:
Wsdl2java-P cxf. Test-d: \ Src-Server HTTP: // localhost: 8080/cxftomcat/services/helloworld? WSDL
3) import the Java interface class to the project.
There are many Java class files generated in the previous step. Generally, you only need to import the class file of the interface to the project. The helloworld. Java file generated in the above example.
3. service call
Method 1: Use JWS high-level encapsulation, such:
PackageCxf. test;
ImportJavax. xml. namespace. QNAME;
ImportJavax. xml. ws. Service;
ImportJavax. xml. ws. Soap. soapbinding;
ImportCxf. Test. helloworld; // necessary
Public Final ClassClient {
Private Static FinalQNAMESERVICE_NAME
=NewQNAME ("http://test.cxf/", "helloworld"); // The first parameter for the interface implementation class package name suffix
Private Static FinalQNAMEPort_name
=NewQNAME ("http://test.cxf/", "helloworldport ");
PrivateClient (){}
Public Static VoidMain (string ARGs [])ThrowsException {
Service = service. Create (SERVICE_NAME );
// Endpoint address
String endpointaddress = "http: // localhost: 8080/cxftomcat/services/helloworld ";
// Add a port to the service
Service. addport (port_name, soapbinding. soap11http_binding, endpointaddress );
Helloworld hW = service. getport (helloworld.Class);
System.Out. Println (HW. Say ("world "));
}
}
Method 2: use lower-level code to control program behavior more accurately, such:
PackageCxf. test;
ImportOrg. Apache. cxf. jaxws. jaxwsproxyfactorybean;
ImportCxf. Test. helloworld; // necessary
Public Final ClassClient {
PrivateClient (){}
Public Static VoidMain (string ARGs [])ThrowsException {
Jaxwsproxyfactorybean factorybean =NewJaxwsproxyfactorybean ();
Factorybean. getininterceptors (). Add (New loggingininterceptor (); (optional)
Factorybean. getoutinterceptors (). Add (New loggingoutinterceptor (); (optional)
Factorybean. setserviceclass (cxf. Test. helloworld.Class);
Factorybean. setaddress ("http: // localhost: 8080/cxftomcat/services/helloworld ");
Helloworld client = (helloworld) factorybean. Create ();
System.Out. Println (client. Say ("God "));
System.Exit(0 );
}
}
Note: loggingininterceptor and loggingoutinterceptor are log interceptors used to display logs during input and output. Using or not does not affect the behavior of the program.
Method 3: Use spring, for example:
PackageCxf. test;
ImportOrg. springframework. Context. Support. classpathxmlapplicationcontext;
ImportCxf. Test. helloworld; // necessary
Public Final ClassClient {
PrivateClient (){}
Public Static VoidMain (string ARGs [])ThrowsException {
Classpathxmlapplicationcontext context =NewClasspathxmlapplicationcontext (NewString [] {"cxf/test/client-beans.xml "});
Helloworld client = (helloworld) Context. getbean ("client ");
String response = client. Say ("Joe ");
System.Out. Println ("response:" + response );
System.Exit(0 );
}
}
Note: To do this with spring, a client-beans.xml must exist in the cxf. Test package, as shown below:
<? 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/schema/jaxws.xsd ">
<Bean id = "client" class = "cxf. Test. helloworld"
Factory-bean = "clientfactory" factory-method = "CREATE"/>
<Bean id = "clientfactory" class = "org. Apache. cxf. jaxws. jaxwsproxyfactorybean">
<Property name = "serviceclass" value = "cxf. Test. helloworld"/>
<Property name = "Address" value = "http: // localhost: 8080/cxftomcat/services/helloworld"/>
</Bean>
</Beans>
4. Execute
Run as Java application