前言
在工作中有些公司是会使用较老的发布方式来对外发布接口,比如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。方便测试接口,直观明了