个人学习到webservice这一块,然后查了资料,主要有cxf和axis这两种框架。cxf又比较容易整合spring,所以本文章就是用cxf来写webservice。写这文章主要目的是为了以后查看方便。
cxf主要分为服务端和客户端2个部分。
服务端的创建过程如下
1、首先创建一个maven项目
File–>new–>Other–Maven–>Maven Project。这里具体怎么创建怎么搜一下百度就好
2、因为maven刚创建时候webapp模块没有东西,pom.xml会报错找不到web.xml,我们可以手动加进去或者用工具生成
3、在pom.xml里面加上cxf需要的包,代码示意如下
<properties>
<spring.version>4.1.3.RELEASE</spring.version>
<cxf.version>3.1.8</cxf.version>
</properties>
<dependencies>
<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>
这样就指定cxf版本,保存后eclipse会自动去下载包到本地的maven库。
接下来就是在web.xml中写入cxf配置
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
4、等maven库加载完后就可以写服务端代码
这里我写了一个IHelloService接口
package com.hll.webservice.service;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
@WebService
public interface IHelloService {
@WebMethod
String sayHello(@WebParam(name="username")String username);
}
这边接口上要写明是一个WebService,方法写WebMethod,参数写WebParam
接下来可以写实现类HelloServiceImpl
package com.hll.webservice.service.impl;
import com.hll.webservice.service.IHelloService;
public class HelloServiceImpl implements IHelloService {
@Override
public String sayHello(String username) {
return "hello " + username;
}
}
5、写完这2个类我们就可以整合spring,首先我们要在pom.xml中引入spring需要的jar包。
这里我因为为了方便也是为了后期开发,把所有spring的包都引入进去,大家可以根据需要的自己引入
<properties>
<spring.version>4.1.3.RELEASE</spring.version>
<cxf.version>3.1.8</cxf.version>
</properties>
<dependencies>
<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>
<!-- springframe start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
<!-- springframe end -->
</dependencies>
导入完jar包就可以写spring整合了,这里我写了一个applicationContext-cxf.xml用于整合cxf和spring
<?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: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">
<!-- Cxf WebService 服务端示例 -->
<jaxws:endpoint id="Helloservice" implementor="com.hll.webservice.service.impl.HelloServiceImpl" address="/hello"/>
</beans>
写完applicationContext-cxf.xml这个文件就可以在web.xml中把这文件加载进去,具体加载如下:
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
这样配置就全部结束了。
6、启动tomcat,然后访问http://localhost:8080/cxf_server/services (cxf_server这个是我的项目名称),如果成功的话会显示如下信息
(https://img-blog.youkuaiyun.com/20170427110952373?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdTAxNDU0OTQwMg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)