cxf 版本:3.1.10
开发环境:jdk 1.6
简单介绍一下cxf的配置过程,如果更深入的了解,可以通过下载的sample当中的example查看更具体的内容。
1,导入jar包,这里同样我把所有cxf下的jar包导入到测试项目当中
2,创建接口文件
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.RPC)
public interface IComplexUserService {
public String getUserByName(@WebParam(name = "name") String name);
public void setUser(String user);
}
3,实现接口文件
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
public class ComplexUserService implements IComplexUserService {
public String getUserByName(@WebParam(name = "name") String name) {
return name;
}
public void setUser(String user) {
System.out.println("############Server setUser###########");
System.out.println("setUser:" + user);
}
}
4,修改Web.xml文件,添加内容如下:
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<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>
5,修改applicaiontContext.xml文件:
<bean id="userServiceBean" class="com.test.service.ComplexUserService" />
<jaxws:server id="userService" serviceClass="com.test.service.IComplexUserService"
address="/Users"> <!-- address 配置为webservice服务地址 -->
<jaxws:serviceBean>
<!-- 要暴露的 bean 的引用 -->
<ref bean="userServiceBean" />
</jaxws:serviceBean>
</jaxws:server>
tip:3.0之后的版本不再需要导入cxf.xml,cxf-extension-soap.xml,cxf-servlet.xml这三个文件。
6,发布webservice,根据配置的信息访问webservice地址:http://localhost:8090/CXFWJS/Users?wsdl,从浏览器中可以看到如下内容:发布成功
客户端调用:
使用wsimport将webservice接口文件下载到本地,并在项目当中引用。下载下来的文件如下:
客户端调用程序如下:
public static void main(String[] args) throws Exception {
//第一种调用
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setServiceClass(IComplexUserService.class);
factory.setAddress("http://localhost:8090/CXFWJS/Users");
IComplexUserService service = (IComplexUserService) factory.create();
System.out.println("#############Client getUserByName##############");
String user = service.getUserByName("test");
System.out.println(user);
service.setUser(user);
//第二种调用方式
IComplexUserServiceService iComplexUserServiceService = new IComplexUserServiceService();
IComplexUserService iComplexUserService = iComplexUserServiceService.getIComplexUserServicePort();
String result = iComplexUserService.getUserByName("testUser");
System.out.println(result);
//第三种调用方式
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
//也可以通过wsdl文件调用
Client client = dcf.createClient("http://localhost:8090/CXFWJS/Users?wsdl");
Object[] result2 = client.invoke("getUserByName", "test User");
System.out.println(result2[0]);
}