javaweb学习路之四--cxf发布Webservice
背景:maven构建的springMvc+mybatis框架
源码---》https://github.com/Zering/MyWeb
步骤:(本步骤是自己在实际探索过程中的步骤,我的思路是先简单粗暴的写出方法,报错了再根据错误来解决问题)
第一步:直接写出了接口和实现类
示例接口代码
package com.app.service; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; @WebService @SOAPBinding(style = Style.RPC) public interface IWebService { public String sayHello(String string); }
示例实现类方法
package com.app.service.impl; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import javax.jws.soap.SOAPBinding.Style; import org.springframework.stereotype.Service; import com.app.service.IWebService; @Service @WebService(endpointInterface="com.app.service.IWebService",serviceName="webservice1") @SOAPBinding(style = Style.RPC) public class WebserviceImpl implements IWebService { @Override public String sayHello(String string) { return "hello "+string; } }
第二步:写cxf的配置信息,创建一个application-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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" 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" default-autowire="byName"> <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="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/> <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/> <jaxws:endpoint implementor="com.app.service.impl.WebserviceImpl" address="/webservice1"></jaxws:endpoint> </beans>
注意:这个文件里面的三个<import>是从jar包中导入的,所以在classpath后面一定要加*号
步骤三:修改web.xml
1.修改<context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml classpath:application-cxf.xml</param-value>
</context-param>
2.追加一个webservice的servelet
<!-- For WebService --> <servlet> <servlet-name>CXFServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/WebService/*</url-pattern> </servlet-mapping>
步骤四:这个时候启动tomcat的话,会有异常说缺啥缺啥的,根据这个信息来导入相应的jar包,加的jar包如下:
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>apache-cxf</artifactId> <version>3.1.6</version> <type>pom</type> </dependency> <dependency>
<groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-bindings-soap</artifactId> <version>3.1.6</version> </dependency>
步骤五:验证,启动tomcat,url输入 http://localhost:8080/MyWeb/WebService
完成。