cxf之HelloWorld版本实例

http://cxf.apache.org/download.html下载CXF,然后加载它的所有jar包。


主要写四个类:接口、实现类、服务端、客户端

package com.cxf;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public interface HelloWorld {
	public String sayHi(@WebParam(name = "text") String text);

	public String sayHiToUser(User user);
}


package com.cxf;

import javax.jws.WebParam;
import javax.jws.WebService;

@WebService
public class HelloWorldImpl implements HelloWorld {
	
	@Override
	public String sayHi(@WebParam(name = "text") String text) {
		return "Hello " + text;
	}

	@Override
	public String sayHiToUser(User user) {
		return "Hello " + user.getName();
	}

}


package com.cxf;

public class User {
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}

package com.cxf;

import javax.xml.ws.Endpoint;

public class Server {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		System.out.println("web service start");
		HelloWorld implementor = new HelloWorldImpl();
		String address = "http://localhost:8080/helloWorld";
		Endpoint.publish(address, implementor);
		System.out.println("web service started");
	}

}

package com.cxf;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class Client {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		JaxWsProxyFactoryBean svr = new JaxWsProxyFactoryBean();
		svr.setServiceClass(HelloWorld.class);
		svr.setAddress("http://localhost:8080/helloWorld");
		HelloWorld hw = (HelloWorld) svr.create();
		User user = new User();
		user.setName("yang");
		System.out.println(hw.sayHiToUser(user));
		System.out.println(hw.sayHi("yang2"));
	}

}

运行服务端:

web service start
2014-1-28 21:40:10 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://cxf.com/}HelloWorldImplService from class com.cxf.HelloWorld
2014-1-28 21:40:11 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:8080/helloWorld
2014-1-28 21:40:11 org.eclipse.jetty.server.Server doStart
信息: jetty-8.1.14.v20131031
2014-1-28 21:40:11 org.eclipse.jetty.server.AbstractConnector doStart
信息: Started SelectChannelConnector@localhost:8080
2014-1-28 21:40:12 org.apache.cxf.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-1-28 21:40:12 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be soap.udp://239.255.255.250:3702
2014-1-28 21:40:12 org.apache.cxf.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
web service started

在浏览器中输入http://localhost:8080/helloWorld?wsdl,可以看到:



运行客户端:

2014-1-28 21:40:51 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://cxf.com/}HelloWorldService from class com.cxf.HelloWorld
Hello yang
Hello yang2

看看wsdl,用@WebParam的参数,是可以看到参数名的,如text,而没有@WebParam的参数,参数名是arg0。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值