使用apache CXF和maven开发Web Service
来源:互联网 发布:wi fi万能钥匙mac版 编辑:程序博客网 时间:2024/05/29 04:37
1、编写服务器类
从Java6开始,WebService API从Java EE复制到了Java SE。并遵循了一系列的标准,比如JSR181(Web Service 元数据),JSR224(JAX-WS,基于XML的WebService API),JSR67(SAAJ,SOAP附件标准)等。 并分别定义到javax.jws, javax.xml.ws 和 javax.xml.soap包中。
JSR181支持使用标注(annotation)来定义WebService。在javax.jws中主要的标注类包括:
1)创建maven工程
2)增加CXF依赖
<!-- cxf -->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.7.18</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>2.7.18</version>
</dependency>
会把cxf相关的jar包加载进去
3)创建服务接口
package com.mar114.cwms.webservice;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
/**
*
* @author LYF
*
*/
@WebService
public interface ICxfServiceDemo {
@WebMethod
public String sayHello(@WebParam(name = "username") String username);
}
4)服务接口实现类
package com.mar114.cwms.webservice.impl;
public class CxfServiceDemoImpl implements ICxfServiceDemo{
public String sayHello(String userName) {
try{
return "Hello "+userName;
}catch( Exception e){
e.printStackTrace();
TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();//手动回滚事务
}
}
}
5)配置WebService接口
- 添加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:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd" >
<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"/>
<jaxws:endpoint id="cxfServiceDemo" implementor="com.mar114.cwms.webservice.impl.CxfServiceDemoImpl"
address="/cxfDemo" > </jaxws:endpoint>
</beans>
- 配置web.xml
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
配置applicationContext.xml(WebService配置事务)
这样就在webService中的方法中添加了一个事务,但是如果方法出现异常我们必须抛出这个异常(即抛出异常给Spring),这样Spring才可以处理这个异常并且回滚,除上述方法之外,我们也可以手动回滚(我写的webService方法用到的就是手动回滚)
5)WebService接口测试
新建一个java project,命名为WebServiceTest
在该项目中创建一个java类,即com.test.CxfServiceDemoTest,测试代码如下:
控制台输出"Hello 张三",说明WebService接口测试成功。
说明:
如果确定webservice接口的访问地址:访问你的服务器地址+web.xml中配置的CXFServlet的拦截地址[我配的是/services/*],即http://192.168.1.131:8080/CWMS/services/【CWMS是项目名】,出现如下内容
点击WSDL后的url,地址变为了http://192.168.1.131:8080/CWMS/services/cxfDemo?wsdl,即为CxfServiceDemoService接口的访问地址
- 使用apache CXF和maven开发Web Service
- 使用apache CXF和maven开发Web Service
- 使用apache CXF和maven开发Web Service
- 使用apache CXF和maven开发Web Service
- 使用apache CXF和maven开发Web Service
- 使用apache CXF和maven开发Web Service
- 使用apache CXF和maven开发Web Service
- Java使用Apache CXF开发Web Service
- 使用Apache CXF开发Web Service步骤
- 使用Apache CXF和Spring集成创建Web Service
- 使用Apache CXF和Spring集成创建Web Service
- 使用Apache CXF和Spring集成创建Web Service
- Apache CXF开发Web Service 目录
- 使用Apache CXF创建Web Service
- 使用Apache CXF创建简单Web Service
- 使用 Apache CXF 实现 Web Service 详解
- 使用Apache CXF实现Web Service
- 使用 Apache CXF 实现 Web Service 详解
- 个人windows工作中故障集合
- A cycle was detected in the build path of project
- Android4.4深入浅出之SurfaceFlinger总体结构
- 在RDO自动安装的OpenStack Liberty单机中搭建能够内外网互访的网络(五)-创建云主机实例
- yii2 RESTful 接口 api -1 : 接口的基本配置
- 使用apache CXF和maven开发Web Service
- 黑马程序员——IO(一)——概述、字符流、字节流、流操作规律 .
- java重载与覆盖
- ull
- java中的位移符
- iOS视图控制器的整体概述
- [转]百度UEditor编辑器(php)
- mac中自动保存git用户名和密码
- ORACLE游标概念讲解