So easy Webservice 7.CXF 发布WebService
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了So easy Webservice 7.CXF 发布WebService相关的知识,希望对你有一定的参考价值。
(一)使用ServerFactoryBean 方式实现发布WS服务
1.新建项目,添加cxf jar包到项目中
2.编写服务实现类
/** * CXF WebService * 不用注解 * @author mlxs * */ public class CXFWebService { public String sayHello(String name){ return "hello," + name; } }
3.编写服务发布类
/** * CXF 使用ServerFactoryBean发布WS服务,采用编码方式 * 这种方式不好:不支持注解,不能修改WSDL文件 * 如设置服务名称无效: * @WebService( * serviceName="cxfHelloService" * ) * @author mlxs * */ public class CXFPublishWS { public static void main(String[] args) { String address = "http://127.0.0.1:2345/cxfHello"; ServerFactoryBean factoryBean = new ServerFactoryBean(); //设置服务地址 factoryBean.setAddress(address); //设置服务实现类 factoryBean.setServiceBean(new CXFWebService()); //发布WS factoryBean.create(); System.out.println(address + "?WSDL"); } }
4.访问WSDL地址:http://127.0.0.1:2345/cxfHello?WSDL
5.总结:
这种方式不好:不支持注解,不能修改WSDL文件
* 如设置服务名称无效:
* @WebService(
* serviceName="cxfHelloService"
* )
(二)下面使用支持注解,支持日志的发布方式:
1.创建CXF WS实现类 可以支持注解:自定义服务名
/** * CXF WebService * @author mlxs * */ @WebService( serviceName="CXFWs1" ) public class CXFWebService { public String sayHello(String name){ return "hello," + name; } }
2.创建发布类
/** * CXF 使用JaxWsServerFactoryBean发布WS服务,采用编码方式 * 这种方式: * 1.支持注解 * 2.可以打印日志 * @author mlxs * */ public class CXFPublishWS { public static void main(String[] args) { String address = "http://127.0.0.1:2345/cxfJaxWsHello"; JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean(); //设置服务地址 factoryBean.setAddress(address); //设置服务实现类 factoryBean.setServiceBean(new CXFWebService()); //支持日志:在有请求进来、和返回给客户端的时候打印日志 factoryBean.getInInterceptors().add(new LoggingInInterceptor()); factoryBean.getInInterceptors().add(new LoggingOutInterceptor()); //发布WS factoryBean.create(); System.out.println(address + "?WSDL"); } }
运行后:
2016-1-27 23:13:37 org.apache.cxf.service.factory.ReflectionServiceFactoryBean buildServiceFromClass 信息: Creating Service {http://jaxws.cxfws.ws.mlxs.com/}CXFWs1 from class com.mlxs.ws.cxfws.jaxws.CXFWebService 2016-1-27 23:13:37 org.apache.cxf.endpoint.ServerImpl initDestination 信息: Setting the server‘s publish address to be http://127.0.0.1:2345/cxfJaxWsHello 2016-1-27 23:13:37 org.eclipse.jetty.util.log.Slf4jLog info 信息: jetty-7.4.5.v20110725 2016-1-27 23:13:37 org.eclipse.jetty.util.log.Slf4jLog info 信息: Started [email protected]127.0.0.1:2345 STARTING 2016-1-27 23:13:37 org.eclipse.jetty.util.log.Slf4jLog info 信息: started o.e.j.s.h.ContextHandler{,null} http://127.0.0.1:2345/cxfJaxWsHello?WSDL
3.访问WSDL地址(Get请求):
控制台日志:
2016-1-27 23:16:30 org.apache.cxf.interceptor.AbstractLoggingInterceptor log 信息: Inbound Message ---------------------------- ID: 1 Address: http://127.0.0.1:2345/cxfJaxWsHello?WSDL Http-Method: GET Content-Type: Headers: {Accept=[text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8], accept-encoding=[gzip, deflate, sdch], Accept-Language=[zh-CN,zh;q=0.8], Cache-Control=[max-age=0], connection=[keep-alive], Content-Type=[null], Host=[127.0.0.1:2345], Upgrade-Insecure-Requests=[1], User-Agent=[Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36]} --------------------------------------
4.使用wsimport下载源码,放到client工程中,执行访问WS接口:
public static void main(String[] args) { CXFWs1 ws = new CXFWs1(); CXFWebService port = ws.getCXFWebServicePort(); System.out.println(port.sayHello("administrator")); }
客户端输出:
hello,administrator
服务端日志: 2016-1-27 23:21:41 org.apache.cxf.interceptor.AbstractLoggingInterceptor log 信息: Inbound Message ---------------------------- ID: 4 Address: http://127.0.0.1:2345/cxfJaxWsHello Encoding: UTF-8 Http-Method: POST Content-Type: text/xml; charset=UTF-8 Headers: {Accept=[text/xml, multipart/related, text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2], connection=[keep-alive], Content-Length=[215], content-type=[text/xml; charset=UTF-8], Host=[127.0.0.1:2345], SOAPAction=[""], User-Agent=[Java/1.6.0_13]} Payload: <?xml version="1.0" ?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:sayHello xmlns:ns2="http://jaxws.cxfws.ws.mlxs.com/"><arg0>administrator</arg0></ns2:sayHello></S:Body></S:Envelope> --------------------------------------
(三)WebService总结
1.ws访问流程: 在调用方法的时候先发送一条get请求去访问远程的wsdl文件(此文件中有相应的公共接口和可以调用的方法),此流程称为"握手"
2.然后在客户端发送 post请求传输 soap数据交给服务器,最后服务器返回soap格式给客户端
3.前面所讲的ws服务都是硬编码的服务, Service应该是单例模式, 如果有Spring,CXF应该需要交给Spring管理
以上是关于So easy Webservice 7.CXF 发布WebService的主要内容,如果未能解决你的问题,请参考以下文章
So easy Webservice 8.spring整合CXF 发布WS