初学CXF,参考官方的文档对Spring3和CXF2.7.10做了一个整合,记录下来以备忘。
一、Spring用到的到包:
二、CXF用到的包:
三、工程示例
四、Web.xml文档
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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_3_0.xsd"
version="3.0"
metadata-complete="true">
<!-- webservice -->
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/CXFService/*</url-pattern>
</servlet-mapping>
<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml </param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
<?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"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- ========================= CXF配置 ================================== -->
<!-- Import Apache CXF Bean Definition 固定配置 -->
<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" />
<!-- 要发布成webservice的bean -->
<jaxws:endpoint id="HelloWorld" address="/HelloWorld"
implementor="com.lee.server.impl.ServiceHelloImpl" />
</beans>
六、WebService接口示例
package com.lee.server;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import com.lee.model.Cat;
import com.lee.model.User;
import com.lee.tools.FkXmlAdapter;
@WebService
public interface ServiceHello {
public String say(String name);
public List<Cat> getCatByUser(User user);
/**
* CXF无法处理Map<String,Cat>,于是用FkXmlAdapter.calss处理
* @return
*/
@XmlJavaTypeAdapter(FkXmlAdapter.class)
public Map<String,Cat> getAllCat();
public String hehe();
}
七、接口实现类示例
package com.lee.server.impl;
import java.util.List;
import java.util.Map;
import javax.jws.WebService;
import com.lee.model.Cat;
import com.lee.model.User;
import com.lee.server.ServiceHello;
import com.lee.server.UserService;
@WebService(endpointInterface="com.lee.server.ServiceHello")
public class ServiceHelloImpl implements ServiceHello{
@Override
public String say(String name) {
// TODO Auto-generated method stub
return name+"你好!";
}
@Override
public List<Cat> getCatByUser(User user) {
// TODO Auto-generated method stub
UserService us = new UserServiceImpl();
return us.getCatByUser(user);
}
@Override
public Map<String, Cat> getAllCat() {
// TODO Auto-generated method stub
UserService us = new UserServiceImpl();
return us.getAllCat();
}
@Override
public String hehe(){
return "hhe";
}
}
八、在Tomcat中运行,浏览器中输入:http://127.0.0.1/WS_EE/CXFService/HelloWorld?wsdl
PS: 在URL中WS_EE为工程名,CXFService为Web.xml中配置的路径,HelloWorld为applicationContext.xml中的address="/HelloWorld"。
示例源码下载地址:http://download.youkuaiyun.com/detail/libinsbox/6949443