项目要用到cxf,对WebService这方面的东西不熟悉,甚至都没有用过!从JavaEye上看到一位大哥把自己录制的视频,甚是高兴,学习一下,补充一下自己!
视频分为7部分,我挨着顺序给写到播客上,希望能够帮助那些想学习cxf又无从下手的朋友!
1. 使用cxf实现简单的HelloWorld
1)使用myeclipse新建一个Java项目,项目名HelloWorld。
2)把下载的cxf-2.3.1的lib下的所有jar文件添加进来,有可能有些文件用不到。
3)建立WebService接口,Package:test,Name:HelloWorld
package test;
public interface HelloWorld {
public String sayHello(String name);
}
4)建立接口的实现类HelloWorldImpl
package test;
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String name) {
//为了表明这个方法确实被调用了,写一个输出语句
System.out.println("Say Hello is called");
return "Hello " + name;
}
}
5)为了把接口和实现发布为WebService,我们需要添加一些标注,最简单的是只标注一个@WebService就行了,别的采用默认,后面再讲
package test;
import javax.jws.WebService;
@WebService
public interface HelloWorld {
public String sayHello(String name);
}
package test;
import javax.jws.WebService;
@WebService
public class HelloWorldImpl implements HelloWorld {
@Override
public String sayHello(String name) {
//为了表明这个方法确实被调用了,写一个输出语句
System.out.println("Say Hello is called");
return "Hello " + name;
}
}
6)接下来建立一个类,用来启动我们的WebService。因为cxf内嵌了一个Jetty,很容易通过Jetty把我们的WebService发布出来。
(1)新建一个工厂
(2)为工厂设置WebService具体实现类,注意是具体实现类
(3)为WebService设置地址
(4)只要通过这样的设置,就能够通过Jetty把我们的WebService发布到这个地址
(5)得到Server
(6)启动Server
package test;
import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
public class MainServer {
public static void main(String[] args) {
JaxWsServerFactoryBean factory = new JaxWsServerFactoryBean();
factory.setServiceClass(HelloWorldImpl.class);
factory.setAddress("http://localhost:8080/HelloWorld");
Server server = factory.create();
server.start();
}
}
7)打开浏览器,输入http://localhost:8080/HelloWorld?wsdl,就可以看到WebService生成的WSDL
该 XML 文件并未包含任何关联的样式信息。文档树显示如下。
−
<wsdl:definitions name="HelloWorldImplService" targetNamespace="http://test/">
−
<wsdl:types>
−
<xs:schema elementFormDefault="unqualified" targetNamespace="http://test/" version="1.0">
<xs:element name="sayHello" type="tns:sayHello"/>
<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>
−
<xs:complexType name="sayHello">
−
<xs:sequence>
<xs:element minOccurs="0" name="name" type="xs:string"/>
</xs:sequence>
</xs:complexType>
−
<xs:complexType name="sayHelloResponse">
−
<xs:sequence>
<xs:element minOccurs="0" name="sayHelloResult" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
</wsdl:types>
−
<wsdl:message name="sayHelloResponse">
<wsdl:part element="tns:sayHelloResponse" name="parameters">
</wsdl:part>
</wsdl:message>
−
<wsdl:message name="sayHello">
<wsdl:part element="tns:sayHello" name="parameters">
</wsdl:part>
</wsdl:message>
−
<wsdl:portType name="HelloWorld">
−
<wsdl:operation name="sayHello">
<wsdl:input message="tns:sayHello" name="sayHello">
</wsdl:input>
<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse">
</wsdl:output>
</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:8080/HelloWorld"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
8)可以看到WebService已经发布成功。
9)接下来为WebService建立一个客户端,新建一个类HelloWorldClient
(1)新建一个Jax代理工厂
(2)为WebService设置地址
(3)设置接口的类型,因为不知道具体类是什么
(4)通过create方法得到HelloWorld
(5)调用HelloWorld方法
10)启动服务器,然后运行HelloWorld的客户端
package test;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class HelloWorldClient {
public static void main(String[] args) {
JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
factory.setAddress("http://localhost:8080/HelloWorld");
factory.setServiceClass(HelloWorld.class);
HelloWorld helloWorld = (HelloWorld) factory.create();
System.out.println(helloWorld.sayHello("KingAragorn"));
}
}
Hello KingAragorn
分享到:
相关推荐
CXF视频:1、使用CXF实现简单的HelloWorld
使用CXF实现简单HelloWorld.swf 主要是让初学者学习cxf使用
CXF视频教程1 使用CXF实现简单的HelloWorld
CXF webservice 入门视频 很清晰很不错,企业当中很多都在使用CXF
2.HelloWorld 3.WSDL描述 WebService CXF学习——进阶篇 1.SOAP讲解 2.JAX-WS讲解 3.对象传递 WebService CXF学习——高级篇(一)(二) 1.整合Spring框架 2.CXF+Spring+Hibernate 3.WS-Security ...
使用apache cxf开发一个入门小程序
本示例只是一个cxf的简单的一个helloWorld程序,运行时先运行webServiceApp.java,再在地址栏中输入http://localhost:8080/helloWorld,如果在浏览器中有一串xml输出则成功,然后再运行HelloWorldClient.java文件...
cxf 的简单实现,maven管理,jdk1.6.
CXF入门的经典项目,SOA实现技术之一。要学SOA,必看。
NULL 博文链接:https://fengfan2008.iteye.com/blog/509342
使用CXF发布和调用webservice之HelloWorld入门使用CXF发布和调用webservice之HelloWorld入门
apache cxf 一个helloworld的例子
使用CXF开发简单HelloWorld实例
利用CXF3.0+Spring3.2 HelloWorld实例,并且导入了小量的jar包以及在写例子的过程中遇见的一些错误!
cxf实用案例代码,helloworld的基本代码,请参阅
NULL 博文链接:https://hbiao68.iteye.com/blog/1968185
webservice-helloworld 视频 两台机器访问,一个是虚拟机。 高清
CXF整合spring,同时支持JSON和XML的CXF配置,支持自动转成实体类,其中的hibernate配置可以不用理会。如果还有不明白可以在留言里留下联系方式,大家可以一起讨论
NULL 博文链接:https://a52071453.iteye.com/blog/1675822
webservice-helloworld 视频 两台机器访问,一个是虚拟机。