初学随记,感觉spring与cxf不会太过耦合,使用的是spring的注入特性而已,所以配置文件可以分开。
1. 先创建接口类和接口实现类
@WebService
@SOAPBinding(style = Style.RPC)
public interface RXFService {
public String getText(@WebParam(name = "s") String[] s);
}
@Service
public class RXFServiceImpl implements RXFService {
@Override
public String getText(String[] s) {
int len = s.length;
String ss = new String();
if(len>0&&len%2==0)
{
for(int i=0;i<len;i+=2)
{
ss += "<"+s[i]+">";
ss += s[i+1];
ss += "</"+s[i]+">";
}
return ss;
}
return "change not success";
}
}
注意:接口的@WebService是cxf需要的,而实现类的@Service是springmvc框架的注解
2.新建cxf.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:soap="http://cxf.apache.org/bindings/soap"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util.xsd
http://cxf.apache.org/bindings/soap
http://cxf.apache.org/schemas/configuration/soap.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.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:server id="eoo" serviceClass="com.picc.easyorder.service.RXFService" address="/eoo"></jaxws:server> -->
<!-- 配置方式1,在impl的@Service(“RXFService”) 里面添加名称,利用spring的注入实现-->
<jaxws:endpoint id="eoo"
implementor="#RXFService"
address="/eoo" />
<!-- <jaxws:server serviceClass="com.picc.easyorder.service.RXFService" address="/eoo"></jaxws:server> -->
<!-- 配置2,直接在此配置文件中绑定-->
<!-- <jaxws:server id="eoo" serviceClass="com.picc.easyorder.service.RXFService" serviceName="hiWS" address="/eoo">
<jaxws:serviceBean>
<bean class="com.picc.easyorder.service.impl.RXFServiceImpl" ></bean>
</jaxws:serviceBean>
</jaxws:server> -->
</beans>
3.web.xml的配置
在最后加入
<!-- cxf服务启动servlet -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<init-param>
<param-name>config-location</param-name>
<param-value>classpath:conf/cxf.xml</param-value> <!-- cxf.xml在conf包下 -->
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/cxf/*</url-pattern>
</servlet-mapping>
测试在浏览器输入localhost:8080/工程名/在web.xml设置的url(此为cxf)/于cxf.xml配置的address
如果后台报错org.apache.cxf.interceptor.Fault: No such operation则在url后加入?wsdl,如http://localhost:8080/testWebService/cxf?wsdl,若还报错,则检查是否接口类与实现类没绑定成功,检查配置文件。
最后附一个spring+cxf的小demo:http://download.youkuaiyun.com/detail/lin245709189/9892017