使用cxf整合spring发布webservice

前言

在工作中有些公司是会使用较老的发布方式来对外发布接口,比如webservice,这里介绍的是使用spring 整合cfx发布webservice提供对外接口给其他公司(或别人)使用。cxf也是目前最流行的一种发布webservice方式。

所需要的jar(因为临时写的,只是svn上截图下来的)
这里写图片描述
这里写图片描述

application.xml配置


<?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:p="http://www.springframework.org/schema/p"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd 
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.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.xml"/>


<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/> -->

<!-- <bean id="vehicleService" class="com.vkl.hblw.service.VehicleService"/> -->
 <jaxws:endpoint id="vehicleService" implementor="com.vkl.hblw.service.VehicleService" address="/vehicleService" />
</beans>

web.xml配置

加入这几句话

     <servlet>  
                     <servlet-name>CXFServlet</servlet-name>  
                     <display-name>CXFServlet</display-name>  
                     <servlet-class>  
                            org.apache.cxf.transport.servlet.CXFServlet  
                     </servlet-class>  
                     <load-on-startup>1</load-on-startup>  
               </servlet>  

               <servlet-mapping>  
                      <servlet-name>CXFServlet</servlet-name>  
                      /**访问时候加入webservice前缀*/
                      <url-pattern>/webservice/*</url-pattern>  
               </servlet-mapping>  

注解方式配置

@WebService
@SOAPBinding(style = Style.RPC)
public class VehicleService {

    @Autowired
    private VehicleDao vehicleDao;
    @Autowired
    private AcceptanceTypeDao acceptanceTypeDao;
    @Autowired
    private StationStaffDao staffDao;
...................
    /**
     * 
     * @Title: UpInspectionSignal
     * @Description: 检测信号接口
     * @author xy
     * @param vit
     * @param stationCode
     * @param inspectionNum
     * @param lineCode
     * @param uniqueString
     *            车辆唯一标识
     * @param time
     *            发送信号时间 格式:yyyy-MM-dd HH:mm:ss
     * @param startOrEnd
     *            开始信号填start,结束信息填stop
     * @return String ResultData的xml格式
     */
    @WebMethod(operationName = "UpInspectionSignal")
    @WebResult(name = "UpInspectionSignalResult")
    @RequestWrapper(localName = "UpInspectionSignal")
    @ResponseWrapper(localName = "UpInspectionSignalResponse")
    public String upInspectionSignal(@WebParam(header = true, name = "VIToken", partName = "viToken") VIToken viToken,
            @WebParam(name = "stationCode") String stationCode, @WebParam(name = "inspectionNum") String inspectionNum,
            @WebParam(name = "lineCode") String lineCode, @WebParam(name = "uniqueString") String uniqueString,
            @WebParam(name = "time") String time,@WebParam(name = "startOrEnd") String startOrEnd) {
        VehicleInspectionService infoUpload = new VehicleInspectionService();
        VehicleInspectionServiceSoap infoUploadService = infoUpload.getPort(VehicleInspectionServiceSoap.class);
        String result = infoUploadService.upInspectionSignal(stationCode, inspectionNum, lineCode, uniqueString, time,
                startOrEnd);
        return result;

    }
其中@WebMethod(operationName = "UpInspectionSignal")--自定义接口别名
@WebResult(name = "UpInspectionSignalResult")--调用接口后自定义返回的结果参数
@RequestWrapper(localName = "UpInspectionSignal")--自定义请求的参数别名
@ResponseWrapper(localName = "UpInspectionSignalResponse")--自定义响应别名
@WebParam(name = "stationCode") String stationCode--自定义请求参数别名
@WebParam(header = true, name = "VIToken", partName = "viToken") VIToken viToken--设置请求头信息(一般用来验证用户登录是否授权信息 VIToken是一个登录返回信息对象,本身携带登录认证信息)

这个时候访问地址为:http://localhost:8080/项目名/webservice/vehicleService?wsdl

推荐使用测试soap工具为soapui。方便测试接口,直观明了

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值