CXF学习(1) 使用CXF实现简单的HelloWorld

项目要用到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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
牙科就诊管理系统利用当下成熟完善的SSM框架,使用跨平台的可开发大型商业网站的Java语言,以及最受欢迎的RDBMS应用软件之一的Mysql数据库进行程序开发。实现了用户在线查看数据。管理员管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等功能。牙科就诊管理系统的开发根据操作人员需要设计的界面简洁美观,在功能模块布局上跟同类型网站保持一致,程序在实现基本要求功能时,也为数据信息面临的安全问题提供了一些实用的解决方案。可以说该程序在帮助管理者高效率地处理工作事务的同时,也实现了数据信息的整体化,规范化与自动化。 管理员在后台主要管理病例管理、字典管理、公告管理、药单管理、药品管理、药品收藏管理、药品评价管理、药品订单管理、牙医管理、牙医收藏管理、牙医评价管理、牙医挂号管理、用户管理、管理员管理等。 牙医列表页面,此页面提供给管理员的功能有:查看牙医、新增牙医、修改牙医、删除牙医等。公告信息管理页面提供的功能操作有:新增公告,修改公告,删除公告操作。公告类型管理页面显示所有公告类型,在此页面既可以让管理员添加新的公告信息类型,也能对已有的公告类型信息执行编辑更新,失效的公告类型信息也能让管理员快速删除。药品管理页面,此页面提供给管理员的功能有:新增药品,修改药品,删除药品。药品类型管理页面,此页面提供给管理员的功能有:新增药品类型,修改药品类型,删除药品类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值