利用Apache CXF 框架简单发布Web服务

利用Apache CXF框架简单发布Web服务

1. 下载Apache CXF见 http://cxf.apache.org/download.html 目前最新版本3.0.0-milestone2

2. CXF压缩包解压到一个路径,然后再设置环境变量:

计算机——属性——高级系统设置——环境变量——在系统变量处选择新建,添加CXF_HOME,值设置为你放CXF的路径,如 E:\apache-cxf-3.0.0-milestone2

再编辑用户变量的Path变量,添加%CXF_HOME%\bin

3. 打开MyEclipse,新建一个java项目,项目目录下新建lib文件夹,将CXFlib目录拷到该文件夹中,然后在Java Build Path中设置Libraries,项目中引入这些jar包。

4. 创建服务接口类HelloWorldService

因为这个接口将会被我们暴露为webservice,所以给该接口加一个@WebService标注

package webService;
import javax.jws.*;

@WebService
public interface HelloWorldService {
	public String sayHello(String userName);
}

5. 创建HelloWorld实现类

package webServiceImpl;


import javax.jws.WebService;


import webService.HelloWorldService;


@WebService(endpointInterface="webService.HelloWorldService",serviceName="helloWorldService")
public class HelloWorldServiceImpl implements HelloWorldService {


	@Override
	public String sayHello(String userName) {
		System.out.println("HelloWorldServiceImpl.sayHello("+userName+")");
		return "Hello,"+userName;
	}


}

6. 创建Server端测试类

package server;
import javax.xml.ws.Endpoint;
import webServiceImpl.HelloWorldServiceImpl;
public class Server {
	public static void main(String[] args){
		System.out.println("Starting Server");
		HelloWorldServiceImpl hwsi=new HelloWorldServiceImpl();
		String address="http://localhost:9000/helloWorldService";
		Endpoint.publish(address, hwsi);
		System.out.println("Starting Success");
	}
}

7. 运行Service.java,直接Run as java application即可,输出如下信息:

Starting Server
2014-4-14 18:17:04 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://webServiceImpl/}helloWorldService from class webService.HelloWorldService
2014-4-14 18:17:06 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:9000/helloWorldService
2014-4-14 18:17:06 org.eclipse.jetty.server.Server doStart
信息: jetty-8.1.14.v20131031
2014-4-14 18:17:06 org.eclipse.jetty.server.AbstractConnector doStart
信息: Started SelectChannelConnector@localhost:9000
2014-4-14 18:17:06 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}Discovery from WSDL: classpath:/org/apache/cxf/ws/discovery/wsdl/wsdd-discovery-1.1-wsdl-os.wsdl
2014-4-14 18:17:06 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be soap.udp://239.255.255.250:3702
2014-4-14 18:17:07 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}DiscoveryProxy from class org.apache.cxf.jaxws.support.DummyImpl
Starting Success

8.发布后,在浏览器里访问链接 http://localhost:9000/helloWorldService?wsdl ,即可得到WSDLWeb服务。 

 

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://webServiceImpl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://webService/" name="helloWorldService" targetNamespace="http://webServiceImpl/">
<wsdl:import location="http://localhost:9000/helloWorldService?wsdl=HelloWorldService.wsdl" namespace="http://webService/"></wsdl:import>
<wsdl:binding name="helloWorldServiceSoapBinding" type="ns1:HelloWorldService">
<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="helloWorldService">
<wsdl:port binding="tns:helloWorldServiceSoapBinding" name="HelloWorldServiceImplPort">
<soap:address location="http://localhost:9000/helloWorldService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

这样就完成了CXFJava生成WSDL的功能,实现的是用户输入一个字符串,调用服务返回一个字符串的简单服务。

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值