spring CXF配置以及WSDL的详解

[size=medium][b][color=red]WSDL它是一种用XML来描述网络服务的语言。[/color][/b][/size]

[color=red]每个wsdl都定义了一项服务(service),而这项服务被定义为一组“端口(port)”。[/color][color=red]你可以把wsdl中的端口想象为URL地址,而不是TCP/IP中的数据通道。[/color]一个端口定义了一个”服务”项目的提供地点。[b]一项服务可以有多个提供地点,但只能对一组事先安排好的“信息(message)”作出响应。[/b]“信息”是对通信数据的描述,每条信息由一组数据组成,这些数据必须定义为收发双方都知道的某种“类型(type)”。如果没有遇到其他方法更能说明问题的情况,则”类型”必须用XSD来定义。端口和信息结合在一起代表了一组“操作(operation)”,并定义了这个端口的“端口类型(portType)”。把一种协议和一种数据格式关联在一起就定义了一种可重复使用的“绑定(binding)”。把一个网络地址和一个绑定关联在一起就定义出了一个端口,而一组端口将定义出一项服务。综上所述,wsdl文档使用下面这些元素来定义一项网络服务:

Type:数据类型定义的包容器。对类型的描述可以用xsd来完成。
[color=red][b]Message:定义通信中的数据。包括数据输入和输出。 [/b][/color]
Operation:对某项服务所能完成的一个动作进行的抽象定义。
[b][color=red]Port:由一个绑定和一个网络地址所定义的一个端点。 [/color][/b]
PortType:对一个或多个端口所支持的一组操作进行描述。
[color=red][b]Binding:为一个给定的端口类型安排协议和数据格式。 [/b][/color]
Service:由一组相互关联的端口所构成的一个聚合。

[color=red][size=medium][b]首先在web.xml文件中加语句:[/b][/size][/color]


<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>

[color=red][b]在classpath下加入 spring-cxf.xml 内容:[/b][/color]

[color=blue]在cxf-2.2.x版本中需要加入cxf目录下的三个文件[/color] [color=red]而2.6.x则只需要加cxf.xml和cxf-servlet.xml[/color]

<?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.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-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<!-- 例子 -->
<bean id="xxWebService" class="com.xx.WebServiceImpl">
<property name="xxService" ref="xxService"/>
</bean>
<jaxws:endpoint
id="xxWebService"
implementor="#xxWebService"
address="/xxWebService" />
<!-- implementor是上面bean的id address为访问WSDL文件的地址 -->
<!--http://localhost:8080/项目名/services/xxWebService?wsdl- ->
</beans>
<!-- END SNIPPET: beans -->


[size=large][color=blue][b]千万别忘记address中的 / 非常关键[/b][/color][/size]


[size=medium][color=red][b]webservice接口[/b][/color][/size]

package com.xx.webservice.xxWebService ;
import javax.jws.WebService;

@WebService
public interface xxWebService {
public String Allxx(xx);
}



[size=medium][color=red][b]WEBSERVICE实现类[/b][/color][/size]


import javax.jws.WebService;

//endpointInterface里面的参数是web接口完整路径
@WebService(endpointInterface = "com.xx.webservice.xxWebService")
public class xxWebServiceImpl implements xxWebService{
private xxService xService;
public String Allxx(xx) {
String xx = xxService.getAllxx(xx);
return xx;
}
}


访问路径
[color=red][b]http://localhost:8080/项目名/services/xxWebService?wsdl[/b][/color]

成功后点击你所要的接口。然后生成wsdl文件。
转换代码之前需要安装
[b]apache-cxf-2.7.3[/b]
然后把生成的文件转换成java代码,转换代码的命令如下:

c:wsdl2java -frontend jaxws21 F:\SecTelephoneService.wsdl

上面生成的文件放在C盘下面。

[size=medium][color=red]客户端访问[/color][/size]

public static void main(String[] args) {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
// 获取服务器端
factoryBean.setAddress("http://localhost:8080/RestFulServer/services/xxWebService");
// 通过客户端的接口获取服务器端的接口
factoryBean.setServiceClass(FirstCxfService.class);
FirstCxfService hello = (FirstCxfService) factoryBean.create();
System.out.println(hello.sayHello("和谐dota"));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值