您的位置:首页 > 编程语言 > Java开发

Web Service (二) CXF自动发布Web Service(No Spring)

2014-11-24 14:54 519 查看
Web Service实现目前流行的框架主要有两种,cxf和axis这两个框架,下面是这两个框架的优缺点,我们这个项目中使用的是cxf这个框架,首先看一下没有集成spring的时候是怎么实现远程调用的。

Axis与Cxf比较

在SOA领域,我们认为Web Service是SOA体系的构建单元(building block)。这两个框架 都是从已有的开源项目发展起来的。这两个框架哪一个更好一些呢?

通过一个比较矩阵来比较Axis2和CXF变得有现实的意义。最主要的区别在以下几个方面:

1.CXF支持 WS-Addressing,WS-Policy, WS-RM, WS-Security和WS-I Basic Profile。Axis2不支持WS-Policy,但是承诺在下面的版本支持。

2. CXF可以很好支持Spring。Axis2不能,这也是我们这个项目中为什么选择cxf来实现web service功能,我们后台使用的是Spring需要同spring集成。

3. AXIS2支持更广泛的数据并对,如XMLBeans,JiBX,JaxMe和JaxBRI和它自定义的数据绑定ADB。

4. Axis2支持多语言-除了Java,他还支持C/C++版本。

CXF发布Web Service实例

1.项目结构

2.接口以及实现类

package com.test.webservice;

public interface IHelloWorld {
public String sayHello(String username);
}


package com.test.webservice;

public class HelloWorldImpl implements IHelloWorld {

@Override
public String sayHello(String username) {

return username+"lilongsheng";
}

}
3.Servlet过滤器

package com.test.webservice;

import javax.servlet.ServletConfig;

import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;

public class WebServlet extends CXFNonSpringServlet{

/**
*
*/
private static final long serialVersionUID = -4585672797532240291L;

@Override
protected void loadBus(ServletConfig sc) {
super.loadBus(sc);

//全局配置
Bus bus = getBus();
//使用全局配置
BusFactory.setDefaultBus(bus);
//接口实现类
HelloWorldImpl helloWorld = new HelloWorldImpl();
//Web server工厂
ServerFactoryBean serverFactoryBean = new ServerFactoryBean();
//设置服务接口类
serverFactoryBean.setServiceClass(IHelloWorld.class);
//服务请求路径
serverFactoryBean.setAddress("/helloWorld");
//设置服务实现类
serverFactoryBean.setServiceBean(helloWorld);
serverFactoryBean.create();

}

}
在这个过滤器中设置了指定哪一个类或者接口可以发布成Web Service方式访问,接口和实现类都设置进入了服务工厂bean里面,调用工厂的创建方法可以创建出WebService对象。

4.web.xml文件配置

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<display-name>Web_Service_No-Spring</display-name>

<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>com.test.webservice.WebServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
</web-app>


5.测试是否成功,在浏览器中输入如下地址:http://localhost:8082/Web_Service_No-Spring/ws,出现如下信息

说明发布Web service接口成功。

浏览器输入:http://localhost:8082/Web_Service_No-Spring/ws/helloWorld?wsdl,将出现配置文件

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://webservice.test.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="IHelloWorld" targetNamespace="http://webservice.test.com/">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://webservice.test.com/" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://webservice.test.com/">
<xsd:element name="sayHello" type="tns:sayHello"/>
<xsd:complexType name="sayHello">
<xsd:sequence/>
</xsd:complexType>
<xsd:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
<xsd:complexType name="sayHelloResponse">
<xsd:sequence>
<xsd:element minOccurs="0" name="return" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</wsdl:types>
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part>
………………………………………………………………………………………………………………
………………………………………………………………………………………………………………


6.客户端测试类

public class testClient {
public static void main(String[] args) {
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:8082/Web_Service_No-Spring/ws/helloWorld?wsdl");
Object[] objects;
try {
objects = client.invoke("sayHello");
//输出调用结果
System.out.println(objects[0].toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}

PS:在客户端调用时总是提示这个错误:


调用webservice出现javax.xml.bind.UnmarshalException解决办法,从网上找了很多解决办法都没有解决,最后根据提示加入了如下两个jar包即解决。

至此通过cxf自动发布Web Service既可以完成。这种方式在容器启动的时候发布好的,可以将整个接口或类发布出去,没有具体知道哪一个方法,显然不够灵活,下一篇介绍cxf手动配置类、方法发布成Web Service,需要哪些方法就可以发布哪些方法灵活一些。

Axis2允许自己作为独立的应用来发布Web Service,并提供了大量的功能和一个很好的模型,这个模型可以通过它本身的架构(modular architecture)不断添加新的功能。有些开发人员认为这种方式对于他们的需求太过于繁琐。这些开发人员会更喜欢CXF。

Axis2出现的时间较早,CXF的追赶速度快。我的建议是:如果你需要多语言的支持,你应该选择AXIS2。如果你需要把你的实现侧重JAVA并希望和Spring集成,CXF就是更好的选择,特别是把你的Web Service嵌入其他的程序中。比如将一个项目中某些类发布成Web Service方式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: