用CXF来做web service是比较简单的,本文就简单介绍一下如何一步步发布web service,以及调用现有的web service。另外如果系统已经使用了Spring MVC,那么引入CXF需要额外的步骤,见本人另外一篇博客http://kyfxbl.iteye.com/blog/1432920。如果展现层没有用spring mvc,而是用struts2之类的,可以省去这一步
首先介绍怎么发布web service:
第1步是创建一个普通的java接口,然后用@WebService注解声明这是一个web service
Java代码 收藏代码
package com.huawei.framework.webservice;
import javax.jws.WebService;
import com.huawei.framework.model.User;
@WebService
public interface HelloWorld {
String sayHi(User user);
}
可以看到,这个接口非常普通,不需要继承什么额外的接口,只需要注解一个@WebService就可以
第2步当然是需要给这个接口提供一个实现类
Java代码 收藏代码
package com.huawei.framework.webservice;
import com.huawei.framework.model.User;
public class HelloWorldImpl implements HelloWorld {
public String sayHi(User theUser) {
return "Hello " + theUser.getName() + " ,your age is "
+ theUser.getAge();
}
}
这个类更加简单,连注解都省了,但是如果这个类实现了不止一个接口,那么就需要加上@WebService注解,不过一般不会这样
第3步就到了cxf出场的时候了,需要在spring配置文件中加上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://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.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="helloWorld"
implementorClass="com.huawei.framework.webservice.HelloWorldImpl"
address="/HelloWorld" />
</beans>
这里只写了CXF所需的配置,spring配置文件的其他内容已省略。用到的元素是<jaxws:endpoint>,implementorClass属性就是我们提供的实现类,然后address属性是这个web service对外暴露的路径
最后第4步是在web.xml中加载cxf
Xml代码 收藏代码
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="MyFramework" version="3.0">
<display-name>MyFramework</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
WEB-INF/beans.xml,
WEB-INF/cxf.xml
</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<servlet>
<servlet-name>framework</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>framework</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
</web-app>
这里写的比较复杂,是为了兼容spring mvc。这里把/webservice/*作为对外暴露的路径,和上面的web service address要结合起来,比如说http://ip:port/app_name/webservice/HelloWorld,就会指向这个例子里声明的web service接口
通过以上4步,就发布了一个web service,在浏览器里输入http://ip:port/app_name/webservice,就会看到这个sayHi的web service了,并且cxf已经自动生成了wsdl文件
然后介绍一下怎么通过cxf调用已有的web service
第1步是需要“获得”web service的对应接口类,这里的“获得”有多种情况。在条件允许的情况下,比如2个子系统都是自己项目组开发的,或者对方愿意提供,那么直接拷贝过来就可以了。但一般是没有这么方便的。。那么这种情况下,就是wsdl发挥作用的时候了。只要对方发布了web service,就一定会有一个wsdl文件,那么可以根据这个wsdl文件来手写还原java接口类和所需的实体类(比如本例中的User模型类),还有一个办法就是用wsdl2java工具,生成所需的类
在这个例子中,我直接把HelloWorld.java拷贝过来就行了,注意拷贝过来的时候,@WebService注解还是需要的
第2步是配置cxf,这里用到的是<jaxws:client>元素,和上面的<jaxws:endpoint>是对应的
Xml代码 收藏代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jaxws="http://cxf.apache.org/jaxws"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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">
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:webservice_address.properties</value>
</list>
</property>
</bean>
<jaxws:client id="helloClient" serviceClass="com.huawei.webservice.client.HelloWorld"
address="${helloworld}" />
</beans>
其中serviceClass属性,就是指向HelloWorld所在的位置,address是目标web service的地址,也就是http://ip:port/app_name/webservice/HelloWorld
这里有一个额外的东西,就是把所有的address,放在单独的webservice_address.properties文件里管理,这样如果用到的web service比较多时,可以集中维护,不需要修改spring配置文件
第3步就是执行,十分简单
Java代码 收藏代码
public class Main {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext(
"cxf.xml");
HelloWorld client = (HelloWorld) context.getBean("helloClient");
User user = new User();
user.setName("zhengshengdong");
String message = client.sayHi(user);
System.out.println(message);
}
}
上面可以看到,首先是启动Spring容器,然后像获取普通的bean一样,用getBean()方法实例化HelloWorld接口,然后直接在其上调用sayHi()方法即可
在这个过程中,CXF对底层的复杂操作进行了封装,包括将实体数据封装成soap格式的消息,然后塞到http的request post里,发送到目标web service。然后从返回的http response中取出soap格式的消息,再反向解析,生成实体对象
分享到:
相关推荐
Springboot整合CXF发布Web service和客户端调用(用户和密码验证)打开及用
Ⅲ)使用EndPoint类的静态方法publish()来发布web service。 2、客户端 Ⅰ)调用CXF提供的wsdl2java工具,根据WSDL文档生成相应的Java代码(任何语言实现web service都要暴露WSDL文档); Ⅱ)找到wsdl2java所...
【精品文档】基于cxf webservice传递List及bean.pdf CXF学习笔记.doc 使用Apache CXF开发Web Service.pdf 如何使用myeclipse开发 webservice
cxf获取第三方天气数据包中内含: MainTest.java, WeatherWS.wsdl,使用说明文档.txt
Google为ndroid平台开发Web Service提供了支持,提供了Ksoap2-android相关架包接下来介绍android调用web service(cxf),感兴趣的朋友可以了解下
CXF实例源代码 客户端调用 web service 入门教程
1. 使用 CXF 发布与调用 REST 服务 2. 请求径注解,包括:@Path,其中包括一个径参数 3. 数据格式注解,包括:@Consumes(输入)、@
1.采用cxf集成Spring实现webservice接口的发布。 2.(调用一)用eclipse根据wsdl连接自动生成java代码实现接口的调用。 3.(调用二)用axis2实现接口调用。 4.开发环境:eclipse+jdk1.8+tomcat8.5
而CXF就是两个项目之间为了提供服务,而开发的一个开源框架,使用CXF框架可以快速搭建web service。CXF就是将项目中暴露出来的接口(服务)包装起来,成为wsdl,使用方通过wsdl来调用这些服务。
04.使用CXF处理JavaBean式的复合类型和List集合类型的形参和返回值
本程序通过CXF-Spring框架来发布一个Webservice(相当于后台,在这里访问数据库),然后由表示层C#来调用其方法。本例子所实现的是从数据库获得的数据通过webservice,显示在C#前台的DataGrid里面。CXF的jar包请本人...
WebService是一种跨编程语言和跨操作系统...它定义了应用程序如何在Web上实现互操作性,你可以用任何你喜欢的语言,在任何你喜欢的平台上写Web service ,只要我们可以通过Web service标准对这些服务进行查询和访问。
主要是webservice系列的电子书,AJAX CXF RESTFUL与ws的相关配合应用知识或书籍
Web Service 本质上是 RPC 调用。以往的 RPC 采用二进制格式进行定义和调用,适合要求效率的同质系统。新的 Web Service 采用 XML 这种人机友好的格式进行定义与调用,在效率与兼容性之间取得了比较好的平衡。 Web ...
Eclipse根据wsdl文件自动生成webservice client图解,以及调用示例,附测试项目。源码示例,包括两个wsdl文件
webService调取IP所在地,以及获得客户端真实IP
使用JAX-WS RI和JAXB的Apache CXF和SpringBoot的SOAP-Webservices-仅注释,绝对没有XML 作为Example SOAP-Service,我做了一些研究,但是毕竟(或因为?)几乎所有教程都使用了众所周知的 ,它是最好的用例。 在...
web service 是java开源的一个远程调用技术,本项目使用了CXF来实现WEB Service的一个案例,同时项目里面有对JAXB的运用,JAXB技术主要运用在java类与xml之间的转换, Marshaller marshaller = context....
Web Service是一种构建应用程序的普遍模型,可以在任何支持网络通信的操作系统中实施运行;它是一种新的webs应用程序分支,是自包含、自描述、模块 化的应用,可以发布、定位、通过web调用。