webservice使用CXF的简单实现
发布时间:2020-12-16 21:41:33 所属栏目:安全 来源:网络整理
导读:webservice使用CXF的简单实现 记录下使用的webservcie 这里是一个简单demo 根据基本知识点,这个是Jax-ws Web Service本身其实是在实现应用程序间的通信。也可以说是两个项目,两台服务器之间。 Web Service平台需要一套协议来实现分布式应用程序的创建。任
webservice使用CXF的简单实现记录下使用的webservcie
其实应该多描述下。。但今天加班有点累。。而且对于简单的demo。。我的语言确实很贫乏,,那就直接贴代码了 代码这是一个模拟的接口: package webservice;
import javax.jws.WebService;
@WebService //webservice sign annotation
public interface IWeatherService {
public String getWeatherByCityName(String cityName) ;
}
实现类,即具体的webservice package webservice;
import javax.jws.WebService;
@WebService //webservice sign annotation
public interface IWeatherService {
public String getWeatherByCityName(String cityName) ;
}
写个main方法跑一下,懒得导Junit package publisher;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean;
import webservice.WeatherService;
public class PublisherTest {
public static void main(String[] args) {
JaxWsServerFactoryBean factoryBean =new JaxWsServerFactoryBean();
factoryBean.setAddress("http://localhost:12345/weather");
factoryBean.setServiceBean(new WeatherService());
factoryBean.create();
}
}
这里新建一个项目,然后把server用的包拷过来。 然后测试跑下 package webservice.invoking;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class WeatherServiceInvokingClient {
public static void testWeather(){
//1.生成一个客户端代理工厂
JaxWsProxyFactoryBean client = new JaxWsProxyFactoryBean();
//2.设置服务端的访问地址
client.setAddress("http://localhost:12345/weather?wsdl");
//3.设置服务端的接口
client.setServiceClass(IWeatherService.class);
//4.创建客户端对象
IWeatherService iws = (IWeatherService) client.create();
//5.调用远程的服务端提供的方法
String result = iws.getWeatherByCityName("Shenyang");
System.out.println(result);
}
public static void main(String[] args) {
testWeather();
}
}
然后结果如下 下次写项目中怎么用,写的不好,有错误请指正。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |