使用CXF创建webservice非常简单,使用步骤如下:
服务端
1、创建接口
@WebService
public interface HelloService {
public @WebResult(name="msg")String sayHello(@WebParam(name="name")String name);
}
2、创建接口的实现类
@WebService(endpointInterface="com.luo.service.HelloService",serviceName="helloService")
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) {
System.out.println("你好"+name);
return "你好"+name;
}
}
3、为了将接口发布出去,在接口和实现类上加上@webservice注解
4、创建服务端发布webservice的主方法:一种是通过CXF内置的Jetty应用服务器发布(见方法一,二),一种是通过tomcat发布(见方法三)
方法一: 最简单,不需要额外的配置,使用sunjax-ws的Endpoint.publish()发布
public class MainServer2 {
public static void main(String[] args) {
Endpoint endpoint = Endpoint.publish("http://localhost:9000/hello",new HelloServiceImpl());
}
}
方法二:用CXF的JaxWsServerFactoryBean类进行发布。(需要CXF相关包)
public class MainServer1 {
public static void main(String[] args) {
JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();
factoryBean.setAddress("http://localhost:9000/hello");//设置发布地址
factoryBean.setServiceClass(HelloService.class);//设置接口
factoryBean.setServiceBean(new HelloServiceImpl());//设置接口的实现类
// factoryBean.getInInterceptors().add(new LoggingInInterceptor());
// factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
factoryBean.create();//发布
}
}
方法三:spring结合cxf,通过tomcat发布(后面讲解)
5、然后访问http://localhost:9000/hello?wsdl查看wsdl报文xml
客户端:
6、客户端代码生成:根据wsdl,使用wsdl2java的命令行生成客户端代码
进入dos窗口,进入cxf下的bin目录下,使用:
wsdl2java -p com.luo.client -d E:/ http://localhost:9000/hello?wsdl
具体:
-p 指定其wsdl的命名空间,也就是要生成代码的包名;
-d 指定要产生代码所在目录;
-client 生成客户端测试web service的代码;
-server 生成服务器启动web service的代码;
-impl 生成web service的实现代码;
-ant 生成build.xml文件;
7、调用web服务
方法一:
使用标准的sunjax-ws的api来完成客户端调用,不需要额外的配置和包
public class MainClient {
public static void main(String[] args) throws MalformedURLException {
QName qName = new QName("http://service.luo.com/","HelloService_Service");
HelloService_Service helloService_Service = new HelloService_Service(new URL("http://localhost:9000/hello?wsdl"),qName);
HelloService helloService = helloService_Service.getPort(HelloService.class);
helloService.sayHello("tom");
}
}
方法二、用CXF中JaxWsProxyFactoryBean客户端代理工厂调用web服务(需要导入CXF相关包)
public class MainClient2 {
public static void main(String[] args){
JaxWsProxyFactoryBean proxyFactoryBean = new JaxWsProxyFactoryBean();
proxyFactoryBean.setAddress("http://localhost:9000/hello");
proxyFactoryBean.setServiceClass(HelloService.class);
HelloService helloService = (HelloService) proxyFactoryBean.create();
helloService.sayHello("tom");
}
}
方法三:
public class MainClient3 {
public static void main(String[] args) {
Service service = Service.create(new QName("http://service.luo.com/"));
service.addPort(new QName("http://service.luo.com/","HelloServiceImplPort") ,SOAPBinding.SOAP11HTTP_BINDING , "http://localhost:9000/hello");
HelloService helloService = service.getPort(HelloService.class);
helloService.sayHello("tom");
}
}
方法四:(需要导入CXF相关包)
JaxWsDynamicClientFactory dcf =JaxWsDynamicClientFactory.newInstance();
org.apache.cxf.endpoint.Client client =
dcf.createClient("http://127.0.0.1:8080/WSCXF/helloService?wsdl");
//sayHello 为接口中定义的方法名称 张三为传递的参数 返回一个Object数组
Object[] objects=client.invoke("sayHello","张三");
//输出调用结果
System.out.println(objects[0].toString());
8、CXF和Spring整合
8.1 添加jar
8.2 web.xml下添加cxf的servlet:将会过滤所有/services/*的url
<!-- CXF WebService --> <servlet> <servlet-name>CXFServlet</servlet-name> <display-name>CXF Servlet</display-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> <load-on-startup>3</load-on-startup> </servlet> <servlet-mapping> <servlet-name>CXFServlet</servlet-name> <url-pattern>/services/*</url-pattern> </servlet-mapping>
8.3 到application-context.xml中添加3个地方:
<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.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" />
可以在原有配置文件上修改
8.4 创建webservice服务端的接口和实现类,平时使用的接口和类一样,只是添加了对应的注解
8.5 在applicaiton-context.xml中添加bean
<jaxws:endpoint id="helloworld" implementor="com.luo.HelloworldImpl" address="/helloworld"/>
8.6 编写客户端WebService的接口,这里还使用先前写的Hello.java
8.7 在spring的配置文件添加如下内容:
<jaxws:client id="helloClient" serviceClass="com.luo.IHelloworld" address="http://localhost:8080/cxfSpring/helloworld"/>
8.8 在代码用调用这个bean:
ServletContext sc = getServletContext();
WebApplicationContext ac = WebApplicationContextUtils.getRequiredWebApplicationContext(sc);
Hello hello = (Hello) ac.getBean("helloClient");
String str = hello.say("zhang");