和jaxws相比,服务器发布方式和客户端访问方式不同
以下是cxf的代码:
服务器发布方式:
package service; import javax.xml.ws.Endpoint; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; /**
* 发布服务
*/
public class FaBu { public static void main(String[] args) {
//发布soap协议的
JaxWsServerFactoryBean jaxWsServerFactoryBean = new JaxWsServerFactoryBean();
//指定webservice地址
jaxWsServerFactoryBean.setAddress("http://127.0.0.1:12345/weather");
//指定portType
jaxWsServerFactoryBean.setServiceClass(WeatherInterface.class);
//指定服务类对象
jaxWsServerFactoryBean.setServiceBean(new Impl());
//发布服务
jaxWsServerFactoryBean.create(); }
}
客户端访问:
package client; import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; import http.pojo.Pojo;
import http.pojo.PojoPortType;
import http.pojo.PojoService; import org.apache.cxf.endpoint.Server;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.apache.cxf.jaxws.JaxWsServerFactoryBean; /**
* 使用cxf的方法
*
*/
public class Cxf_client1 { public static void main(String[] args) {
//发布soap协议的
JaxWsProxyFactoryBean jaxWsServerFactoryBean = new JaxWsProxyFactoryBean();
//调用webservice地址
jaxWsServerFactoryBean.setAddress("http://127.0.0.1:12345/weather");
//设置portType
jaxWsServerFactoryBean.setServiceClass(PojoPortType.class);
//创建portType
PojoPortType pojoPortType = (PojoPortType) jaxWsServerFactoryBean.create();
List<Pojo> queryWeather = pojoPortType.queryWeather("郑州");
//解析
for (Pojo pojo : queryWeather) {
System.out.println(pojo.getDetail());
Date date = pojo.getDate().toGregorianCalendar().getTime();
System.out.println(new SimpleDateFormat("yyMMdd").format(date));
System.out.println(pojo.getTemperatureMax());
System.out.println(pojo.getTemperatureMin()); }
}
}