加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

基于CFX WebService做的一个简单的HelloWorld例子

发布时间:2020-12-17 01:14:56 所属栏目:安全 来源:网络整理
导读:package test;import javax.jws.WebService;@WebServicepublic interface HelloWorld {public String sayHello(String name);} package test;import javax.jws.WebService;@WebServicepublic class HelloWorldImpl implements HelloWorld {public String say
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 {

	public String sayHello(String name) {
		System.out.println("sayHello is called");
		return "Hello "+name;
	}

}
package test;

import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

public class MainServer {

	
	public static void main(String[] args) {
		
		JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
		HelloWorld hello = new HelloWorldImpl();
		factoryBean.setServiceBean(hello);
		factoryBean.setServiceClass(HelloWorldImpl.class);
		factoryBean.setAddress("http://127.0.0.1:8080/HelloWorld");
		
		Server server = factoryBean.create();
		server.start();
	}

}


package test;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

public class HelloWorldClient {

	
	public static void main(String[] args) {

		JaxWsProxyFactoryBean bean = new JaxWsProxyFactoryBean();
		bean.setAddress("http://127.0.0.1:8080/HelloWorld");
		bean.setServiceClass(HelloWorld.class);
		
		HelloWorld world = (HelloWorld) bean.create();
		System.out.println(world.sayHello("LCY"));
	}

}

注:网上有些视频例子里面给的MainServer类里面 只写了factoryBean.setServiceClass(HelloWorldImpl.class);?

应该还要再前面加上

HelloWorld hello = new HelloWorldImpl();

factoryBean.setServiceBean(hello);

不然会报异常,异常情况见:http://topic.csdn.net/u/20110809/15/a39454e4-4c73-4ef6-9a34-5b30608eed4f.html?62533

解决问题来源于:http://www.opensourceforce.org/forum/viewthread.php?action=printable&tid=2613

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读