Endpoint endpoint =
Endpoint.publish("http://localhost:8080/helloService",new HelloServiceImpl());
System.out.println("WS发布成功!");
HelloServiceImpl impl = new HelloServiceImpl();
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
factoryBean.setAddress("http://localhost:8080/WSCXF/helloService");//发布地址
factoryBean.setServiceClass(IHelloService.class);//接口类
factoryBean.setServiceBean(impl);//发布接口的实现类的实例
factoryBean.create();
System.out.println("WS发布成功!");
方法三:使用配置式的,该方法既需要导入CXF的相关jar包,也需要进行配置文件的书写,这种方法适用于与spring整合的应用中,因为可以将bean托管给spring,也就可以随心所欲的使用spring中配置的bean了,以下是application-server.xml文件的配置信息
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jaxws="http://cxf.apache.org/jaxws" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" /> <import resource="classpath:META-INF/cxf/cxf-servlet.xml" /> <!--cxf配置信息--> <bean id="helloServiceBean" class="org.cxf.service.HelloServiceImpl"/> <jaxws:endpoint id="helloService" implementor="#helloServiceBean" address="/hello"> </jaxws:endpoint> </beans>
JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();
soapFactoryBean.setAddress("http:// localhost:8080/helloService");
soapFactoryBean.setServiceClass(IHelloService.class);
Object o = soapFactoryBean.create();
IHelloService helloService = (IHelloService)o;
helloService.sayHello();//sayHello()为暴露的接口中的一个方法;
http://localhost:8080/WSCXF/helloService //为暴露的接口的名称
?wsdl 必须要添加,这个是接口解析成的xml文件
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
Client client =
dcf.createClient("http://localhost:8080/WSCXF/helloService?wsdl");
//sayHello为接口中定义的方法名称张三为传递的参数返回一个Object数组
Object[] objects=client.invoke("sayHello", "张三");
//输出调用结果
System.out.println(objects[0].toString());