cxf 框架 webservice
cxf 内置了一个web服务器
cxf简单入门实例
package test; import org.apache.cxf.jaxws.JaxWsServerFactoryBean; import com.itheima.logic.MyLogic; public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JaxWsServerFactoryBean bean=new JaxWsServerFactoryBean();创建实例 bean.setAddress("http://localhost:12345/mylogic");设置地址 bean.setServiceBean(new MyLogic());将webservice对象传入 bean.create();发布 } }
package test; import org.apache.cxf.jaxws.JaxWsProxyFactoryBean; import com.itheima.logic.MyLogic; public class Main { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JaxWsProxyFactoryBean bean=new JaxWsProxyFactoryBean();创建实例 bean.setAddress("http://localhost:8080/cxfspringserver/ws/mylogic?wsdl");获取wsdl说明书 bean.setServiceClass(MyLogic.class);生成的接口。class MyLogic mylogic= (MyLogic) bean.create(); int x=mylogic.add(100, 200); System.out.print(x); } }
没有用到cxf框架时候,在客户端进行开发需要
package test; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.xml.ws.Service; import com.itheima.service.Weather; public class Main2 { /** * @param args * @throws MalformedURLException */ public static void main(String[] args) throws MalformedURLException { // TODO Auto-generated method stub //指出到哪里去找 URL url=new URL("http://localhost:12345/weather?wsdl"); //找什么 QName qname=new QName("http://service.itheima.com/","WeatherService"); 第一个参数是命名空间,wsdl中找,第二个参数是wsdl里面的service 后的name //service视图 Service service=Service.create(url,qname); //得到portType Weather weather= service.getPort(Weather.class); System.out.print(weather.getInfo("北京")); } }