spring 4.0下集成webservice
该教程使用的项目可参见:
Intellij Idea下搭建基于Spring+SpringMvc+MyBatis的WebApi接口架构
具体源码请参见GitHub:
https://github.com/chenyangsocool/ssm.git
1.在maven的pom文件中添加cxf版本属性信息:
<cxf.version>3.0.8</cxf.version>
添加如下cxf的jar包:
<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标签,最后文件如下:
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文件,内容如下:
<?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>
再添加:
<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
参考(这一篇基本上是照搬了,感谢原作者):
spring4.1.6+cxf3.0.8的简单WebService案例(maven工程)
** Then I looked up at the sky and saw the sun **
posted on 2018-03-15 18:38 chenyangsocool 阅读(357) 评论(0) 编辑 收藏 举报