学习了一下WebService, 做了个小例子,以供参考:
1. 下载 apache-cxf-2.2.12.zip 包,地址: http://cxf.apache.org/
2. 创建 java 工程,导入相关的jar包.
3. 创建一个接口(使用注解声明webservice @WebService )
package test;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHello(String name);
}
4. 实现创建的接口
package test;
import javax.jws.WebService;
@WebService
public class HelloWorldImpl implements HelloWorld {
public String sayHello(String name) {
// TODO Auto-generated method stub
System.out.println("接口实现类!");
return "Hello "+name+" ! ";
}
}
5. 创建WebService服务器端
package test;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class MainServer {
/**
* 创建一个服务端
*/
public static void main(String[] args) {
// 创建一个WebService服务端工厂类
JaxWsServerFactoryBean factory =new JaxWsServerFactoryBean();
//定义webService具体实现类,而不是接口
factory.setServiceClass(HelloWorldImpl.class);
//发布webServer到这个地址
factory.setAddress("http://localhost:9001/HelloWorld");
//得到Server,并启动
Server server= factory.create() ;
server.start();
}
}
启动服务器端程序, IE浏览器中 http://localhost:9001/HelloWorld?WSDL
可以看到, 服务器端正常
<?xml version="1.0" encoding="UTF-8" ?>
- <wsdl:definitions name="HelloWorldImplService" targetNamespace="http://test/" xmlns:ns1="http://schemas.xmlsoap.org/wsdl/soap/http" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://test/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
- <wsdl:types>
- <xsd:schema attributeFormDefault="unqualified" elementFormDefault="unqualified" targetNamespace="http://test/" xmlns:tns="http://test/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="sayHello" type="tns:sayHello" />
- <xsd:complexType name="sayHello">
- <xsd:sequence>
<xsd:element minOccurs="0" name="arg0" type="xsd:string" />
</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="sayHello">
<wsdl:part element="tns:sayHello" name="parameters" />
</wsdl:message>
- <wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters" />
</wsdl:message>
- <wsdl:portType name="HelloWorld">
- <wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello" />
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse" />
</wsdl:operation>
</wsdl:portType>
- <wsdl:binding name="HelloWorldImplServiceSoapBinding" type="tns:HelloWorld">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
- <wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document" />
- <wsdl:input name="sayHello">
<soap:body use="literal" />
</wsdl:input>
- <wsdl:output name="sayHelloResponse">
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
- <wsdl:service name="HelloWorldImplService">
- <wsdl:port binding="tns:HelloWorldImplServiceSoapBinding" name="HelloWorldImplPort">
<soap:address location="http://localhost:9001/HelloWorld" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
6. 创建webService客户端
package test;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class HelloClient {
/**
* 创建WebService客户端
*/
public static void main(String[] args) {
// 创建代理工厂类(客户端)
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://localhost:9001/HelloWorld");
//客户端定义实现接口
factory.setServiceClass(HelloWorld.class);
HelloWorld hello= (HelloWorld)factory.create();
System.out.println(hello.sayHello("中 国"));
}
}
最后启动服务器端程序MainServer,客户端HelloClient ,会显示
2011-7-24 17:42:00 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://test/}HelloWorldService from class test.HelloWorld
Hello 中 国 !
此时WebService发布成功!
- 大小: 48.6 KB
分享到:
相关推荐
现在主流的webService框架,一个是axis2,一个是cxf,本资源是基于cxf框架的简单例子
webService CXF不依赖Spring集成,包含客户端、服务端代码。使用3.1.7版的CXF包。demo可以直接运行
借鉴网上例子,弄了一个SpringBoot开发WebService之CXF示例。
webservice cxf示例工程集成spring,包含所有jar包和测试类。使用的是myeclipse8.5
webservice cxf 整合spring例子源代码.相关博客的介绍: http://blog.csdn.net/dream_broken/article/details/35331949
readme下为开发文档。 1.集成 spring 2.集成 WSS4J 3.mtom 功能示例 4.基本数据类型service接口 示例 5.复杂数据类型service接口 示例
webservice示例 springmvc+maven+cxf发布webservice 博客地址 http://blog.csdn.net/joe_storm/article/details/78839150
webservice cxf 里面的分为2部分 服务器端项目 客户端demo 测试个各种传递参数的test类 适合初学者 情况readme txt
完整可用的spring整合CXF示例项目,包括服务发布端和调用端两个项目,包括CXF拦截器的实现的简单的授权校验
webservice天气预报例子cxf实例,
cxf+spring 实现websevice的maven项目,此例子简单,易懂,对刚接触cxf webservice的非常有帮助!
CXF WebService整合Spring示例工程代码demo可以直接导入eclipse。参照网页http://www.cnblogs.com/hoojo/archive/2011/03/30/1999563.html 完成的webService服务提供。 大致步骤: 1.引入cxf和其他需要的jar包,(本...
一个简单的WebService CXF 示例
用camel-cxf调用webservice和发布一个webservice接口例子,首先启动QueryServiceMain主函数启动webservice接口,然后启动测试类TestWebservice。例子主要是实现java代码实现camel调用webservice接口
CXF+Struts+spring的示例
基于spring+cxf实现用户文件传输的webservice.docx,webservice的小例子,对学习很有用,欢迎大家下载
cxf组件 webservice示例代码
使用cxf wsdl2java生成webservice客户端命令
springboot+cxf实现webservice示例 <groupId>org.springframework.boot <artifactId>spring-boot-starter <groupId>org.springframework.boot <artifactId>spring-boot-starter-web <!-- CXF ...
JAVA CXF webservice的示例代码 Eclipse工程