CXF3.0.1+spring+maven搭建webservice服务

来源:互联网 发布:cf刷雷神软件免费版 编辑:程序博客网 时间:2024/06/12 01:19
统一接口:
@WebService
public interface DrugInterfaceService {
public String printMessage(@WebParam(name = "firstName") String firstName,
@WebParam(name = "secondName") String secondName);

}


实现接口:
@WebService(name = "drugService")
public class DrugService implements DrugInterfaceService {


@Override
public String printMessage(String firstName, String secondName) {
return firstName + secondName;
}


}


spring配置文件1:applicationContext-cxf.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:context="http://www.springframework.org/schema/context"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:aop="http://www.springframework.org/schema/aop" 
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-2.5.xsd
http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd  
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
">
<!-- 服务 -->
<jaxws:endpoint id="drugService" implementor="drugService.services.DrugService"
address="drugService" />
</beans>


spring配置文件2:applicationContext.xml:
<beans>
....
</beans>


web.xml配置:


<context-param>  
<param-name>contextConfigLocation</param-name>  
<param-value>/WEB-INF/classes/applicationContext.xml,
/WEB-INF/classes/applicationContext-cxf.xml</param-value> 
</context-param><!-- 多个spring配置文件用,分隔-->


<!-- 初始化  CXF SERVLET-->
<servlet>
<servlet-name>CXFServlet</servlet-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>
<url-pattern>/*</url-pattern>
</servlet-mapping>




pom.xml配置:(一定要加这个才行,不然会报错)


<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.1</version>
</dependency>


然后将项目发布到tomcat启动起来,访问http://localhost:8080/drug/drugService?wsdl。
打印出如下:


<?xml version="1.0" encoding="UTF-8"?>


-<wsdl:definitions targetNamespace="http://services.drugService/" name="DrugServiceService" xmlns:ns1="http://interfaceService.drugService/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://services.drugService/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">


<wsdl:import namespace="http://interfaceService.drugService/" location="http://localhost:8080/drug/drugService?wsdl=DrugInterfaceService.wsdl"> </wsdl:import>
-<wsdl:binding name="DrugServiceServiceSoapBinding" type="ns1:DrugInterfaceService">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
-<wsdl:operation name="printMessage">
<soap:operation style="document" soapAction=""/>
-<wsdl:input name="printMessage">
<soap:body use="literal"/>
</wsdl:input>
-<wsdl:output name="printMessageResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
-<wsdl:service name="DrugServiceService">
-<wsdl:port name="drugServicePort" binding="tns:DrugServiceServiceSoapBinding">
<soap:address location="http://localhost:8080/drug/drugService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>


至此webservice服务创建成功。
0 0
原创粉丝点击