您的位置:首页 > 其它

webservice开发---------如何使用cxf发布soap方式的web服务

2018-09-12 17:08 1276 查看

使用CXF框架发布web服务,简单易用,下面结合实际案例讲解

一:配置web.xml,CXFServlet 负责截获/pubinterface/services/*的请求

[code]<servlet>
<description>Apache CXF Endpoint</description>
<display-name>cxf</display-name>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/pubinterface/services/*</url-pattern>
</servlet-mapping>

二:配置cxf对应的beans.xml文件,address为  /OrderInterfaceService

[code]<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:util="http://www.springframework.org/schema/util"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.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" />
<!--发布的接口-->
<jaxws:endpoint id="OrderInterfaceService"
implementor="#orderInterfaceServiceImpl" address="/OrderInterfaceService">
<jaxws:features>
<bean class="org.apache.cxf.feature.LoggingFeature" />
</jaxws:features>
</jaxws:endpoint>
</beans>

其中jaxws 是配置 SOAP 方式的 Web 服务,有 jaxws:server、jaxws:endpoint、 jaxws:client 三个元素,jaxws:server 和 jaxws:endpoint 是等效的,都用于发布 Web 服务,出 现 jaxws:endpoint 的原因是 JAX-WS 规范中使用 EndPoint 发布 Web 服务,CXF 为了和 JAX-WS 对应,提供了这个与 jaxws:server 功能一样的配置元素;与jaxws可以一样配置的有jaxrs,jaxrs 是 REST 方式的 Web 服务,有 jaxrs:server、jaxrs:client 两个元素。

三:接口定义方法

[code]@WebService
public interface OrderInterfaceService {

/**
* @param inputInfo
* @return
*/
@WebMethod(operationName = "applyOrder")
@WebResult(name = "outputInfo")
String applyOrder(@WebParam(name="inputInfo") String inputInfo);

/**
* @param inputInfo
* @return
*/
@WebMethod(operationName = "orderProcessDetail")
@WebResult(name = "outputInfo")
String orderProcessDetail(@WebParam(name="inputInfo") String inputInfo);

}

四:实现类

[code]@Service
public class OrderInterfaceServiceImpl implements OrderInterfaceService {

//实现类中的方法体无具体意义

@Override
public String applyOrder(String inputInfo) {
return "Interface method : applyOrder, param : " +inputInfo;
}

@Override
public String orderProcessDetail(String inputInfo) {
//TODO
return "Interface method : orderProcessDetail, param : " +inputInfo;
}

}

启动服务,访问发布地址,如案例中项目名称为soc,则wsdl地址为:http://localhost:8080/soc/pubinterface/services/OrderInterfaceService?wsdl,如果为wsdl文档即正确发布

阅读更多
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: