First, CXF and spring consolidation need to prepare the following jar package files:
This way, I'm using Spring's jar package, which is provided by spring, and does not use the spring jar file in CXF.
After adding so many files, first add the following configuration in Web. xml:
<!--load Spring container configuration--
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--set Spring container Load profile path--
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-server.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
Then in the SRC directory, create a new Applicationcontext-server.xml file with the following file contents:
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:jaxws= "Http://cxf.apache.org/jaxws"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://www.springframework.org/schema/beans >
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd
Http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd"
Notice the underlined bold part above, this is very important oh! Can't write wrong or miss out.
After adding this file, you will need to import so many files into this file. The contents of the file are as follows:
<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"/>
The following begins to write server-side code, the first custom server-side interface, the code is as follows:
Package com.hoo.service;
Import Javax.jws.WebParam;
Import Javax.jws.WebService;
Import javax.jws.soap.SOAPBinding;
Import Javax.jws.soap.SOAPBinding.Style;
Import Com.hoo.entity.User;
Import Com.hoo.entity.Users;
/**
* <b>function:</b> custom interface required for client request WebService
* @author Hoojo
* @createDate 2011-3-18 08:22:55
* @file Complexuserservice.java
* @package Com.hoo.service
* @project Cxfwebservice
* @blog Http://blog.csdn.net/IBM_hoojo
* @email [email protected]
* @version 1.0
*/
@WebService
@SOAPBinding (style = Style.rpc)
Public interface Icomplexuserservice {
Public User Getuserbyname (@WebParam (name = "name") String name);
public void SetUser (user user);
}
The following is a WebService implementation class, the server-side implementation code is as follows:
Package com.hoo.service;
Import java.util.ArrayList;
Import Java.util.Date;
Import Java.util.HashMap;
Import java.util.List;
Import Javax.jws.WebParam;
Import Javax.jws.WebService;
Import javax.jws.soap.SOAPBinding;
Import Javax.jws.soap.SOAPBinding.Style;
Import Com.hoo.entity.User;
Import Com.hoo.entity.Users;
/**
* <b>function:</b> WebService transfer complex objects such as JavaBean, Array, List, map, etc.
* @author Hoojo
* @createDate 2011-3-18 08:22:55
* @file Complexuserservice.java
* @package Com.hoo.service
* @project Cxfwebservice
* @blog Http://blog.csdn.net/IBM_hoojo
* @email [email protected]
* @version 1.0
*/
@WebService
@SOAPBinding (style = Style.rpc)
@SuppressWarnings ("deprecation")
public class Complexuserservice implements Icomplexuserservice {
Public User Getuserbyname (@WebParam (name = "name") String name) {
User user = new user ();
User.setid (New Date (). getseconds ());
User.setname (name);
User.setaddress ("China");
User.setemail (name + "@hoo. com");
return user;
}
public void SetUser (user user) {
System.out.println ("########### #Server setuser###########");
System.out.println ("SetUser:" + user);
}
}
Note that the integration with spring, it is necessary to complete the implementation of the interface, if there is no interface, there will be errors.
Here are the following configurations to add to the Applicationcontext-server.xml file:
<bean id= "Userservicebean" class= "Com.hoo.service.ComplexUserService"/>
<bean id= "Inmessageinterceptor" class= "Com.hoo.interceptor.MessageInterceptor" >
<constructor-arg value= "Receive"/>
</bean>
<bean id= "Outlogginginterceptor" class= "Org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<!--attention to the address below, where the address is the name of the access WebService--
<jaxws:server id= "UserService" serviceclass= "Com.hoo.service.IComplexUserService" address= "/users" >
<jaxws:serviceBean>
<!--a reference to the bean to be exposed--
<ref bean= "Userservicebean"/>
</jaxws:serviceBean>
<jaxws:inInterceptors>
<ref bean= "Inmessageinterceptor"/>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean= "Outlogginginterceptor"/>
</jaxws:outInterceptors>
</jaxws:server>
After starting the Tomcat server, request in WebBrowser:
http://localhost:8080/CXFWebService/Users?wsdl
If you can see the contents of the WSDL XML file, you are successful, note that the users of the above address are the names of the addresses in the XML configuration above, which corresponds to one by one.
Write the code for the client request below, as follows:
Package com.hoo.client;
Import Org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
Import Com.hoo.entity.User;
Import Com.hoo.service.IComplexUserService;
/**
* <b>function:</b> request Spring to integrate CXF WebService client
* @author Hoojo
* @createDate 2011-3-28 03:20:35
* @file Springuserswsclient.java
* @package com.hoo.client
* @project Cxfwebservice
* @blog Http://blog.csdn.net/IBM_hoojo
* @email [email protected]
* @version 1.0
*/
public class Springuserswsclient {
public static void Main (string[] args) {
Call WebService
Jaxwsproxyfactorybean factory = new Jaxwsproxyfactorybean ();
Factory.setserviceclass (Icomplexuserservice.class);
Factory.setaddress ("Http://localhost:8080/CXFWebService/Users");
Icomplexuserservice service = (icomplexuserservice) factory.create ();
System.out.println ("############ #Client getuserbyname##############");
User user = Service.getuserbyname ("Hoojo");
SYSTEM.OUT.PRINTLN (user);
User.setaddress ("China-guangzhou");
Service.setuser (user);
}
}
After running, you can see in the console
Log4j:warn No Appenders could is found for logger (org.apache.cxf.bus.spring.BusApplicationContext). Log4j:warn Initialize the log4j system Properly.log4j:WARN see http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.2011-3-28 18:12:26 Org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildservicefromclass Info: Creating Service {Http://service.hoo.com/}icomplexuserserviceservice from class Com.hoo.service.IComplexUserService ############ #Client getuserbyname############# #27 #hoojo#[email protected] #chinaTomcat控制台
This server side is configured through spring integration, and the client side is also integrated through the spring configuration.
First add the Applicationcontext-client.xml configuration file, the file content is as follows:
<?xml version= "1.0" encoding= "UTF-8"?>
<beans xmlns= "Http://www.springframework.org/schema/beans"
xmlns:context= "Http://www.springframework.org/schema/context"
Xmlns:jaxws= "Http://cxf.apache.org/jaxws"
Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"
xsi:schemalocation= "Http://www.springframework.org/schema/beans >
Http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
Http://www.springframework.org/schema/context
Http://www.springframework.org/schema/context/spring-context-3.0.xsd
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"/>
address= "Http://localhost:8080/CXFWebService/Users"/>
</beans>
The client request code is as follows:
Package com.hoo.client;
Import Org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
Import Com.hoo.entity.User;
Import Com.hoo.service.IComplexUserService;
/**
* <b>function:</b> request Spring to integrate CXF WebService client
* @author Hoojo
* @createDate 2011-3-28 03:20:35
* @file Springuserswsclient.java
* @package com.hoo.client
* @project Cxfwebservice
* @blog Http://blog.csdn.net/IBM_hoojo
* @email [email protected]
* @version 1.0
*/
public class Springuserswsclient {
public static void Main (string[] args) {
ApplicationContext CTX = new Classpathxmlapplicationcontext ("Applicationcontext-client.xml");
Icomplexuserservice service = Ctx.getbean ("Userwsclient", Icomplexuserservice.class);
System.out.println ("############ #Client getuserbyname##############");
User user = Service.getuserbyname ("Hoojo");
SYSTEM.OUT.PRINTLN (user);
User.setaddress ("China-guangzhou");
Service.setuser (user);
}
}
After the run, the results are as follows:
############ #Client getuserbyname############# #45 #hoojo#[email protected] #china ########### #Server setuser######## # # #setUser: 45#hoojo#[email protected] #China-guangzhou
CXF WebService Integration Spring