該教程使用的項目可參見:html
Intellij Idea下搭建基於Spring+SpringMvc+MyBatis的WebApi接口架構java
具體源碼請參見GitHub:git
https://github.com/chenyangsocool/ssm.gitgithub
1.在maven的pom文件中添加cxf版本屬性信息:web
<cxf.version>3.0.8</cxf.version>
添加以下cxf的jar包:spring
<dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-ws-metadata_2.0_spec</artifactId> <version>1.1.3</version> </dependency>
2.在ITestService上添加@WebService標籤,最後文件以下:apache
package com.chenyangsocool.ssm.service; import com.chenyangsocool.ssm.model.Test; import javax.jws.WebService; @WebService public interface ITestService { public Test getModelById(int id); }
3.在resources目錄下新建spring-cxf.xml文件,內容以下:mybatis
<?xml version="1.0" encoding="UTF-8"?> <!-- START SNIPPET: beans --> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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://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-servlet.xml"/> <jaxws:endpoint id="TestService" implementor="com.chenyangsocool.ssm.service.impl.TestServiceImpl" address="/TestService"/> </beans>
4.在web.xml的<context-param>中添加spring-cxf.xml,最後以下:架構
<param-value> classpath:spring-mybatis.xml classpath:spring-cxf.xml </param-value>
再添加:app
<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>/webservice/*</url-pattern> </servlet-mapping>
OK,到此結束,運行你的項目,輸入:http://localhost:8080/ssm/webservice
裏面就有你添加的服務:TestService:
http://localhost:8080/ssm/webservice/TestService?wsdl
本項目GitHub地址:https://github.com/chenyangsocool/ssm.git
參考(這一篇基本上是照搬了,感謝原做者):