先贴一段网上很多的代码, 这种方式能发布成功,但是就是调用的时候,参数为null,但是调用成功了..
Bus b=getBus();
BusFactory.setDefaultBus(b);
HelloImpl hi=new HelloImpl();
ServerFactoryBean sfb=new ServerFactoryBean();
sfb.setServiceClass(Hello.class);
sfb.setAddress("/hi");
sfb.setServiceBean(hi);
sfb.create();
调用方式如下,按照以上的方式部署,参数传不进去,也出不来
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:8080/ws/hi?wsdl");
Object[] invoke = client.invoke("say", "123");
System.out.println("我的值是");
System.out.println(invoke[0]);
在往上搜索了很久.换个方式发布,完美解决了问题
protected void loadBus(ServletConfig sc) {
super.loadBus(sc);
HelloImpl hi=new HelloImpl();//实锟斤拷锟斤拷
BusFactory.setDefaultBus(getBus());
Endpoint.publish("/hi", hi);
}
最后贴上整个no-spring代码
/**
*
*/
package test.cxf;
import javax.servlet.ServletConfig;
import javax.xml.ws.Endpoint;
import org.apache.cxf.BusFactory;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;
import org.apache.cxf.transport.servlet.CXFNonSpringServlet;
/**
* @author wangzih
*
*/
public class CXFServlet extends CXFNonSpringServlet {
<span style="white-space:pre"> </span>
<span style="white-space:pre"> </span>public static void main(String[] args) throws Exception {
// JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
// factory.setServiceClass(Hello.class);
// factory.setAddress("http://localhost:8080/ws/hi?wsdl");
// Hello client = (Hello)factory.create();
// client.say("foo");
JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:8080/ws/hi?wsdl");
Object[] invoke = client.invoke("say", "123");
System.out.println("我的值是");
System.out.println(invoke[0]);
}
@Override
protected void loadBus(ServletConfig sc) {
super.loadBus(sc);
HelloImpl hi=new HelloImpl();
BusFactory.setDefaultBus(getBus());
Endpoint.publish("/hi", hi);
}
}
其中web.xml中配置方法
<servlet>
<servlet-name>cxfServlet</servlet-name>
<servlet-class>test.cxf.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>cxfServlet</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
需要引入4个jar包,往上很多人报 一个类
com.sun.tools.internal.xjc.api.XJC
找不到,必须引入第4个包就解决了
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-core</artifactId>
<version>2.6.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http-jetty</artifactId>
<version>2.6.1</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-xjc</artifactId>
<version>2.1.6</version>
</dependency>
在调用的时候,如果发现operation找不到,解决的办法是把接口和实现文件放在同一个包下
或者通过注解
实现类 要加
@WebService(targetNamespace = “http://service.com/“,
endpointInterface = “com.qlm.webservice.service.CodeInfoManager”)
一个指定命令空间,一个指定接口
接口也要指定命令空间
@WebService(targetNamespace = “http://service.com/“)
这样即使不在同一个包下面 也可以发布成功
在接口中,方法的参数也可以添加注解
@WebMethod(operationName=”initialRegion”)
String initialRegion(@WebParam(name=”code”)String code);
这样在wsdl文件中 参数就有名称了,不然默认就是arg0,1等等