- 浏览: 1089256 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
skyesx:
这是2PC实现,更常用的是一个柔性事务的实现,可以参考http ...
Spring分布式事务实现 -
ddbird:
这第一句就不严谨“分布式事务是指操作多个数据库之间的事务”,显 ...
Spring分布式事务实现 -
呵呵6666:
基于互联网支付系统的微服务架构分布式事务解决方案http:// ...
Spring分布式事务实现 -
小黄牛:
写得不错,交流群:472213887
Spring分布式事务实现 -
jiaoqf321456:
这明明是用的apache的压缩,给ant.jar有半毛钱关系吗 ...
使用ant.jar进行文件zip压缩
一、在服务器端的WebSerivce服务发布
1、POM.xml文件引入相关的依赖包
2、定义服务
3、定义访问权限验证的拦截器
4、发布webserive服务
5、SpringBoot应用启动
二、客户端测试调用WebService
1、调用WebSerivce时,设置帐号密码的拦截器
2、客户端调用测试
1、POM.xml文件引入相关的依赖包
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>net.logcd</groupId> <artifactId>springboot_wscxf</artifactId> <version>1.0.0</version> <packaging>jar</packaging> <name>springboot_wscxf</name> <description></description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.8.RELEASE</version> <relativePath/> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.7</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web-services</artifactId> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.6</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.6</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
2、定义服务
@WebService(targetNamespace="http://cxf.logcd.net") @Service("helloService") public class HelloService{ public String sayHello(String user, String userData) { return "hello," + user+"! userData = "+userData; } }
3、定义访问权限验证的拦截器
/** * 校验(用户名、密码)拦截器 */ public class AuthInterceptor extends AbstractPhaseInterceptor<SoapMessage> { private Logger logger = Logger.getLogger(this.getClass()); private static final String USERNAME = "admin"; private static final String PASSWORD = "admin"; public AuthInterceptor() { // 定义在哪个阶段进行拦截 super(Phase.PRE_PROTOCOL); } @Override public void handleMessage(SoapMessage soapMessage) throws Fault { List<Header> headers = null; String username = null; String password = null; try { headers = soapMessage.getHeaders(); } catch (Exception e) { logger.error("getSOAPHeader error: {}", e); } if (headers == null) { throw new Fault(new IllegalArgumentException("找不到Header,无法验证用户信息")); } // 获取用户名,密码 for (Header header : headers) { SoapHeader soapHeader = (SoapHeader) header; Element e = (Element) soapHeader.getObject(); NodeList usernameNode = e.getElementsByTagName("username"); NodeList pwdNode = e.getElementsByTagName("password"); username = usernameNode.item(0).getTextContent(); password = pwdNode.item(0).getTextContent(); if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) { throw new Fault(new IllegalArgumentException("用户信息为空")); } } // 校验用户名密码 if (!(username.equals(USERNAME) && password.equals(PASSWORD))) { SOAPException soapExc = new SOAPException("认证失败"); logger.info("用户认证信息错误"); throw new Fault(soapExc); } } }
4、发布webserive服务
/** * 配置并发布WebService */ @Configuration public class WSCxfConfig { @Bean public ServletRegistrationBean dispatcherServlet() { //接口访问的过滤路径 return new ServletRegistrationBean(new CXFServlet(), "/cxf/*"); } @Bean(name = Bus.DEFAULT_BUS_ID) public SpringBus springBus() { return new SpringBus(); } @Bean public HelloService helloService() { return new HelloService(); } @Bean public Endpoint endpoint() { EndpointImpl endpoint = new EndpointImpl(springBus(), helloService()); endpoint.getInInterceptors().add(new AuthInterceptor());//添加校验拦截器 endpoint.publish("/helloService"); return endpoint; } }
5、SpringBoot应用启动
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
二、客户端测试调用WebService
1、调用WebSerivce时,设置帐号密码的拦截器
/** * 调用时添加用户名密码信息 */ public class LoginInterceptor extends AbstractPhaseInterceptor<SoapMessage> { private String namespace = "http://cxf.cdmcs.com"; private String username = "admin"; private String password = "admin"; public LoginInterceptor(String username, String password) { // 设置在发送请求前阶段进行拦截 super(Phase.PREPARE_SEND); this.username = username; this.password = password; } @Override public void handleMessage(SoapMessage soapMessage) throws Fault { List<Header> headers = soapMessage.getHeaders(); Document doc = DOMUtils.createDocument(); Element auth = doc.createElementNS(namespace, "SecurityHeader"); Element UserName = doc.createElement("username"); Element UserPass = doc.createElement("password"); UserName.setTextContent(username); UserPass.setTextContent(password); auth.appendChild(UserName); auth.appendChild(UserPass); headers.add(0, new Header(new QName("SecurityHeader"), auth)); } }
2、客户端调用测试
public class TestClientCXFWS { /** * 动态调用方式 * @throws Exception */ @Test public void invokeHelloService_WS() throws Exception { //WSDL路径 String wsUrl = "http://127.0.0.1:8080/cxf/helloService?wsdl"; String nameSpace = "http://cxf.logcd.net"; //方法名 String method = "sayHello"; JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance(); Client client = dcf.createClient(wsUrl); //需要密码的情况需要加上用户名和密码 client.getOutInterceptors().add(new LoginInterceptor("admin","admin")); Endpoint endpoint = client.getEndpoint(); //endpoint.getService().getName().getNamespaceURI() QName opName = new QName(nameSpace, method); BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding(); //如果命名空间不对,需要重新寻找operationName if (bindingInfo.getOperation(opName) == null) { for (BindingOperationInfo operationInfo : bindingInfo.getOperations()) { if (method.equals(operationInfo.getName().getLocalPart())) { opName = operationInfo.getName(); break; } } } Object[] params = new Object[] {"Lucy","{time:"+new Date().toString()+"}"}; Object[] retArr = client.invoke(opName, params); System.out.println(retArr[0]); } }
发表评论
-
SpringBoot开发WebService之Axis
2019-07-14 23:56 4648一、服务器端发布WebService服务 1、POM.xml文 ... -
SpringBoot项目非web方式启动
2019-07-03 17:02 46881、springboot 1.x中以非web方式启动 @S ... -
SpringBoot使用Druid数据库密码加密
2019-03-06 23:28 14341、生成公钥、私钥和密码加密串 java -cp drui ... -
JDK6开发WebService及用户认证
2015-08-21 13:50 1383一、编写服务端并发布 1、定义接口 public inte ... -
WebService之CXF使用Session
2015-08-20 13:53 1946一、接口与实现 @WebService public i ... -
WebService之CXF处理类型转换
2015-08-20 13:53 2704一、接口与实现 @WebService @SOAPBin ... -
以多种方式访问WebService
2015-08-20 13:58 6379一、Java访问WebService (1)使用Axis ... -
Spring Annotation
2010-12-02 17:14 0Spring2.x引入了组件自动扫描机制,可以在类路径底 ... -
Exporting beans as web services using XFire
2015-08-20 14:07 1108Once XFire is in your buil ... -
Spring分布式事务实现
2010-11-10 14:28 83139分布式事务是指操作多个数据库之间的事务,spring的 ... -
Spring3 Annotation + Hibernate3-jpa2.0 + CGLIB + 多数据源
2010-08-19 09:30 10482一、定义一个测试用Entity。 @Entity pub ... -
使用iBatis2.0
2010-05-26 10:20 0一、NULL问题 ibatis操作oracle数据库时, ... -
Spring中的远程访问和web服务
2010-05-23 23:17 4481一、介绍 目前,Sp ... -
使用AspectJ LTW(Load Time Weaving)
2010-01-04 14:25 10722在Java 语言中,从 ... -
Spring2.0 AOP AspectJ 注释实现
2010-01-04 14:24 5541一、AOP基本概念 切面(Aspect): 一个关注点的模块 ... -
Spring + JPA + Hibernate配置
2010-01-04 14:24 34696<1>persistence.xml放到类路径下的 ... -
配置spring数据源
2009-11-06 16:47 1215配置一个数据源 Spring在第三方依赖包中包含了两 ... -
集成axis开发webservice
2009-09-16 09:09 3000一、基础知识 Web S ... -
hibernate的dialect
2009-07-23 10:04 5407一、hibernate的dialect RDBM ... -
spring ibatis入门
2009-04-20 14:16 3842一、applicationContext.xml <?x ...
相关推荐
借鉴网上例子,弄了一个SpringBoot开发WebService之CXF示例。
SpringBoot+Mybatis+CXF框架,实现Restful api与 WebService api接口的大实验
springboot整合CXF发布webservice和客户端调用 项目中每个类都有详细的注释,保证都能看懂,不失为一个学习springboot整合cxf来学习webservice发布调用的好例子
基于cxf,能够调用第三方天气接口,服务端接口可以传输普通字符串、json字符串、文件上传和下载功能,满足基本的开发需求
SpringBoot+mybatis+CXF开发web service接口,数据库用的是mysql,表结构在图片中,附上的还有自动生成的客户端代码
主要介绍了详解Spring boot+CXF开发WebService Demo,非常具有实用价值,需要的朋友可以参考下
启动后访问http://localhost:8080/cxf-ws-restful-spring-server/ws63
本实例工程使用Apache CXF组件快速开发WebService。基于Spring框架,使用了Maven项目,但由于时间原因,只使用了Maven Project的框架,还是使用lib文件夹存放所需的cxf库,传入传出对象支持Json格式。
基于SpringBoot + Spring + Apache CXF +Mybatis 开发SOAP的 WebService 服务 # 备注 | **版本** | **说明**| | ------ |:------:| | 1.0.0版本| | | 2.0.0版本|springboot启动| # 原理 Mybatis基于动态代理实现...
这是模拟企业用户的开发,在远程调用的时候客户端必须传入密码,这里通过cxf框架 的自定义拦截器实现jax-ws,jax-rs下次在学习下。 创建springboot项目: 导入起步依赖以及junit pom文件如下: org.spring...
详细介绍3种方法根据wsdl逆向生成webservice服务端;有最原始的wsdl2java指令,还有通过soapUI工具以及通过myeclipse的web service project来生成。
主要介绍了Spring boot 整合CXF开发web service示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
在eclipse中用apache cxf开发的webservice 演示
本课程讲解CXF的JAX-WS方式开发WebService的服务端和客户端?本课程讲解CXF的JAX-RS注解方式开发WebService的服务端和客户端?本课程讲解SpringBoot整合CXF开发WebService
公司生产环境,java开发的webService源码,可用于demo学习。基于springboot2版本开发,代码中注入的实现类可自行修改,需要添加依赖包 <groupId>org.apache.cxf <artifactId>cxf-spring-boot-starter-jaxws ...
四、CXF框架可以与spring无缝连接,就不用我们自己Endpoint了。它还能记录日志之类的 五、我们还可以使用Idea下的webservice,能够使用图形画面的方式获取本地代理和生成WSDL文件。 Activiti 介绍 ...