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());