因为本人项目是在已有的ssh项目上二次开发集成,则对于ssh框架的搭建不在做解释。
一、导入cxf所需jar包
二、编写接口
<span style="font-size:18px;">package com.xxx.cxf;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface CxfWsdl {
public String getCurrentOnline(@WebParam(name="xml") String xml);
}</span>
注意,使用@WebParam(name="xml")来解决生成wsdl时参数名为arg0的问题
三、编写实现类
注意的是和Spring集成,这里一定要完成接口实现,如果没有接口的话会有错误的。
<span style="font-size:18px;">package com.xxx.cxf;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService(endpointInterface = "com.xxx.cxf.CxfWsdl")
public class CxfWsdlImpl implements CxfWsdl {
@WebMethod
@Override
public String getCurrentOnline(String xml) {
System.out.println("-----getCurrentOnline----"+xml);
return "getCurrentOnline";
}
}</span>
四、配置web.xml
<span style="font-size:18px;"><!-- 加载Spring容器配置 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<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>
<!--配置cxf访问路径 -->
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/cxf/*</url-pattern>
</servlet-mapping></span>
注意,其中的配置文件路径文件名可自己命名
配置applicationContext-server.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: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"
划线的为新增加配置语句
<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"/>
导入文件,其中这三个文件在l三个文件都在cxf-2.0.7.jar中把它们拷贝到META-INF/目录下,粘贴复制即可
<bean id="cxfImpl" class="com.xxx.cxf.CxfWsdlImpl"/>
<bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor">
<!-- <constructor-arg value="receive"/> -->
</bean>
<bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
<!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
<jaxws:serverid="cxfWsdl" serviceClass="com.xxx.cxf.CxfWsdl" address="/cxfWsdl">
<jaxws:serviceBean>
<!-- 要暴露的 bean 的引用 -->
<ref bean="cxfImpl"/>
</jaxws:serviceBean>
<jaxws:inInterceptors>
<ref bean="inMessageInterceptor"/>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<ref bean="outLoggingInterceptor"/>
</jaxws:outInterceptors>
</jaxws:server>
截图如下:
访问地址
http://localhost:8080/hrss3/cxf/cxfWsdl?wsdl
其中cxf为web中配置的拦截路径
cxfWsdl为application文件中配置的address