(一)使用 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 SelectChannelConnector@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管理
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com