服务器端的实例是从网上找的,修改了Spring版本和CXF版本。
一、POM配置
通过maven管理项目,pom配置了所用的库。
<properties>
<!-- spring版本号 -->
<spring.version>4.0.6.RELEASE</spring.version>
<cxf.version>3.1.8</cxf.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</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-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-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<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-bindings-soap</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-ws-security</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.codehaus.woodstox</groupId>
<artifactId>stax2-api</artifactId>
<version>3.1.1</version>
2、开发服务器端接口
package com.moon.cxfWebservice.server; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface Greeting { public String greeting(@WebParam(name="username")String userName); }
然后开发实现类
package com.moon.cxfWebservice.server; import java.util.Calendar; import javax.jws.WebService; @WebService(endpointInterface="com.moon.cxfWebservice.server.Greeting") public class GreetingImpl implements Greeting{ public String greeting(String userName) { return "Hello " + userName + ", currentTime is " + Calendar.getInstance().getTime(); } }
至此,服务端的代码就开发完成了。
3、在web.xml和spring配置文件中配置
首先,在web.xml中,需配置servlet,如下图配置:
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
另外,还行配置spring相关的上下文监听,如:
context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config/spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
在web.xml中,声明的spring的配置文件是spring.xml,所以,在Spring.xml中,还需要配置webservice相关的服务。
<bean id="greetingImpl" class="com.moon.cxfWebservice.server.GreetingImpl"/>
<jaxws:endpoint id="greeting" implementor="#greetingImpl" address="/Greeting" />
<bean id="hello" class="com.cigna.cmc.cxf.service.impl.HelloWorldImpl" />
<jaxws:endpoint id="helloWorld" implementor="#hello" address="/HelloWorld">
</jaxws:endpoint>
其中,address是访问的链接中对应该服务的标识。如访问helloworld的配置,访问的完整地址为:
http://localhost:9080/cxfWSServer/webservice/HelloWorld?wsdl
以上,服务器端的开发就完成了。
4、开发客户端
客户端的访问方式,通过查找资源,一共实现了三种,
第一种方式:
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(Greeting.class); factory.setAddress("http://localhost:9080/cxfWSServer/webservice/Greeting?wsdl"); Greeting service = (Greeting) factory.create(); String name = service.greeting("白嘉轩先生"); System.out.println("********返回值="+name);
第二种访问方式,动态访问指定的方法:
String wsUrl = "http://localhost:9080/cxfWSServer/webservice/HelloWorld?wsdl"; DynamicClientFactory objDynamicClientFactory = JaxWsDynamicClientFactory.newInstance(); Client objClient = objDynamicClientFactory.createClient(wsUrl); String method="sayHello"; Endpoint endpoint = objClient.getEndpoint(); QName opName = new QName(endpoint.getService().getName().getNamespaceURI(), method); BindingInfo bindingInfo = endpoint.getEndpointInfo().getBinding(); if (bindingInfo.getOperation(opName) == null) { for (BindingOperationInfo operationInfo : bindingInfo.getOperations()) { if (method.equals(operationInfo.getName().getLocalPart())) { opName = operationInfo.getName(); break; } } } try{ Object[] object = objClient.invoke(opName, "白嘉轩","F"); for(int i=0;i<object.length;i++){ System.out.println("*********object="+object[i].toString()); } }catch (Exception e){ e.printStackTrace(); }
第三种访问方式:
该访问方式是将需要访问的服务配置在Spring项目的配置文件中,之后通过配置文件找到对应的服务。
在Spring的配置文件中配置:
<jaxws:client id="helloWorld" serviceClass="com.cmdi.ws.HelloWorld" address="http://localhost:9080/cxfWSServer/webservice/HelloWorld?wsdl"/>
其中,id的值为在加载了Spring配置文件后,通过id找到对应的服务。
serviceClass为该服务的接口类,应该是在客户端有一个该接口类文件,在服务器端有一个相同的实现类文件。address为访问服务的地址,且“?wsdl”可以不加上,经验证均可以正确调用webservice。
此外,对于客户端需要访问的服务器端的服务,客户端都需要存在该服务的接口类,同时,客户端的接口类需要指明该服务的命名空间。如
@WebService(targetNamespace = "http://impl.service.cxf.cmc.cigna.com/") public interface HelloWorld { @WebResult(name = "String") String sayHello(@WebParam(name = "name") String name, @WebParam(name = "sex") String sex); void test(); }
targetNamespace指定了命名空间,根据观察,该命名空间应该是:
http://+该服务类在服务器端的完整路径的倒叙。
如com.cigna.cmc.cxf.service.impl.HelloWorld
相关推荐
NULL 博文链接:https://chilongxph.iteye.com/blog/510707
基于CXF实现JAVA_WebService.doc Apache CXF 提供方便的Spring整合方法,可以通过注解、Spring标签式配置来暴露Web Services和消费Web Services
1. 前端,如JAX-WS,与核心代码的彻底分离。 2. 简单易用,例如,创建客户端和端点不需标注。...在面向服务的架构(SOA)基础设施项目中,CXF通常和Apache ServiceMix,Apache Camel以及Apache ActiveMQ一起使用。
springboot+cxf实现webservice示例 <groupId>org.springframework.boot <artifactId>spring-boot-starter <groupId>org.springframework.boot <artifactId>spring-boot-starter-web <!-- CXF ...
Apache Cxf WebService整合Spring 处理Map、非javabean式的复合类等CXF无法自动转化的类型 CXF为服务器端和客户端添加自定义拦截器进行权限检查验证并且控制台打印日志
Apache CXF = Celtix + XFire,Apache CXF 的前身叫 Apache CeltiXfire,现在已经正式更名为 Apache CXF 了,以下简称为 CXF。...Spring 进行无缝集成。 想了解新技术的就下下来看看把,应该对你有所帮助。
使用 CXF 做 webservice 简单例子 Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻...
WebService—CXF发布服务spring+cxf的doc文档,以及apache-cxf用到的的jar包
CXF就是一个WebService的框架,在生产环境中一般情况下我们都使用框架来开发,这个框架简单的说就是将WebService的开发给简化了,而且还新增了拦截器。本文将带大家利用Apache CXF快速实现一个WebService。
webserviceApache CXF java springboot利用Apache CXF创建webserice接口 Apache CXF 核心架构是以BUS为核心,整合其他组件。 * Bus是CXF的主干, 为共享资源提供一个可配置的场所,作用类似于Spring的...
弹簧-CXF-WS 一些使用spring和cxf生成webservice Rest和Soap的例子
Apache CXF是一个开源的Service框架,它实现了JCP与Web Service中一些重要标准。CXF简化了构造,集成,面向服务架构(SOA)业务组件与技术的灵活复用。在CXF中,Service使用WSDL标准定义并能够使用各种不同的消息格式...
CXF WebService整合Spring示例工程代码demo可以直接导入eclipse。参照网页http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html 完成的webService服务提供。 大致步骤: 1.引入cxf和其他需要的jar包,(本...
Apache CXF 是一个开放源代码框架,提供了用于方便地构建和开发 Web 服务的可靠基础架构。它允许创建高性能和可扩展的服务,您可以将这样的服务部署在 Tomcat 和基于 Spring 的轻量级容器中,以及部署在更高级的...
Apache CXF 提供方便的Spring整合方法,可以通过注解、Spring标签式配置来暴露Web Services和消费Web Services 各种类型的Annotation。@WebService和@WebMethod是WSDL映射Annatotion。这些Annotation将描述Web ...
这是讲解cxf的权威开发文档 文档内容:Apache CXF 提供方便的Spring整合方法,可以通过注解、Spring标签式配置来暴露Web Services和消费Web Services
主要介绍了springboot整合cxf发布webservice以及调用的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
1. 发布和调用WebService: 使用CXF2.4(http://cxf.apache.org)和spring 2. 调用安全性: 使用简单的USERNAME_TOKEN 3. 服务程序中取得调用者身份 ------------------------- 接口 ------------------------- intf....
从apache官网下载的cxf3.1.8中有150多个jar包,好多是用不上的,我精简了一下,然后写了demo,测试过没问题