栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

webservice(3)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

webservice(3)

客户端配置方式:

JAXRSClientFactoryBean factory = new JAXRSClientFactoryBean();

factory.setServiceClass(RestService.class);

factory.setAddress(url);

factory.getInInterceptors().add(new LoggingInInterceptor());

factory.getOutInterceptors().add(new LoggingOutInterceptor());

RestService ser = factory.create(RestService.class);

ser.get();

输出日志形式如下:客户端:

[INFO ] 03-22 22:33:39 org.apache.cxf.interceptor.AbstractLoggingInterceptor.log(AbstractLoggingInterceptor.java:250) Outbound Message

---------------------------

ID: 1

Address: http://localhost:8080/webapp/ws/cxf/rest

Http-Method: GET

Content-Type: application/xml

Headers: {Content-Type=[application/xml], Accept=[text/plain]}

--------------------------------------

[INFO ] 03-22 22:33:39 org.apache.cxf.interceptor.AbstractLoggingInterceptor.log(AbstractLoggingInterceptor.java:250) Inbound Message

----------------------------

ID: 1

Response-Code: 200

Encoding: ISO-8859-1

Content-Type: text/plain

Headers: {content-type=[text/plain], Date=[Wed, 22 Mar 2017 14:33:39 GMT], transfer-encoding=[chunked]}

Payload: this is default get plain

--------------------------------------

服务端:

[INFO ] 03-22 22:33:39 org.apache.cxf.interceptor.AbstractLoggingInterceptor.log(AbstractLoggingInterceptor.java:250) Inbound Message

----------------------------

ID: 4

Address: http://localhost:8080/webapp/ws/cxf/rest

Encoding: UTF-8

Http-Method: GET

Content-Type: application/xml

Headers: {Accept=[text/plain], cache-control=[no-cache], connection=[keep-alive], content-type=[application/xml], host=[localhost:8080], pragma 《大厂前端面试题解析+Web核心总结学习笔记+企业项目实战源码+最新高清讲解视频》无偿开源 徽信搜索公众号【编程进阶路】 =[no-cache], user-agent=[Apache CXF 3.0.3]}

--------------------------------------

[INFO ] 03-22 22:33:39 org.apache.cxf.interceptor.AbstractLoggingInterceptor.log(AbstractLoggingInterceptor.java:250) Outbound Message

---------------------------

ID: 4

Response-Code: 200

Content-Type: text/plain

Headers: {Content-Type=[text/plain], Date=[Wed, 22 Mar 2017 14:33:39 GMT]}

Payload: this is default get plain

--------------------------------------

自定义拦截器

CXF中也可以自定义拦截器,CXF中实现自定义拦截器需要继承AbstractPhaseInterceptor或者其子类如AbstractSoapInterceptor。

一个简单的拦截器:

public class MyInterceptor extends AbstractPhaseInterceptor{

//必需提供一个带参数的构造函数

public MyInterceptor(String phase){

super(phase);

}

//覆写拦截后的动作

@Override

public void handleMessage(Message message) throws Fault{

Q.p(“~~~~~~~~~~~~~~~~~~”);

if (message.getDestination() != null) {

Q.p(message.getDestination().getAddress());

}

if (message.getExchange() != null) {

Q.p(message.getExchange().getInMessage());

Q.p(message.getExchange().getOutMessage());

}

Q.p(message.getContent(String.class));

Q.p(“~~~~~~~~~~~~~~~~~~~”);

}

}

添加到客户端拦截器链:

JAXRSClientFactoryBean factory = new JAXRSClientFactoryBean();

factory.setServiceClass(RestService.class);

factory.setAddress(url);

factory.getInInterceptors().add(new MyInterceptor(Phase.RECEIVE));

factory.getOutInterceptors().add(new MyInterceptor(Phase.SEND));

ser = factory.create(RestService.class);

ser.get();

输出:

[DEBUG] 03-22 23:07:38 org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:304) Invoking handleMessage on interceptor cn.lg.ws.MyInterceptor@58cbafc2

~~~~~~~~~~~~~~~~~~

null

{org.apache.cxf.message.Message.PROTOCOL_HEADERS={Content-Type=[application/xml], Accept=[text/plain]}, org.apache.cxf.transport.Conduit=conduit: class org.apache.cxf.transport.http.URLConnectionHTTPConduit1715248762target: http://localhost:8080/webapp/ws/cxf/rest, org.apache.cxf.request.uri=http://localhost:8080/webapp/ws/cxf/rest, jaxrs.template.parameters=null, org.apache.cxf.message.Message.ENDPOINT_ADDRESS=http://localhost:8080/webapp/ws/cxf/rest, jaxrs.proxy=true, org.apache.cxf.request.method=GET, org.apache.cxf.transport.processOneWayResponse=true, http.connection=sun.net.www.protocol.http.HttpURLConnection:http://localhost:8080/webapp/ws/cxf/rest, org.apache.cxf.message.Message.BASE_PATH=http://localhost:8080/webapp/ws/cxf/, org.apache.cxf.client=true, org.apache.cxf.invocation.context={ResponseContext={}, RequestContext={org.apache.cxf.message.Message.PROTOCOL_HEADERS={Content-Type=[application/xml], Accept=[text/plain]}, org.apache.cxf.request.uri=http://localhost:8080/webapp/ws/cxf/rest, BODY_INDEX=-1, org.apache.cxf.message.Message.ENDPOINT_ADDRESS=http://localhost:8080/webapp/ws/cxf/rest, jaxrs.proxy=true, org.apache.cxf.jaxrs.model.OperationResourceInfo=org.apache.cxf.jaxrs.model.OperationResourceInfo@117159c0}}, java.lang.annotation.Annotation=[Ljava.lang.annotation.Annotation;@19e4653c, org.apache.cxf.message.inbound=false, http.scheme=http, Content-Type=application/xml, org.apache.cxf.empty.request=true}

null





[![复制代码](https://img-blog.csdnimg.cn/img_convert/ed413f7149fca62da7a526af92160faa.gif)](https://blog.csdn.net/swazer_z/article/details/72902721 "复制代码")



---



[![复制代码](https://img-blog.csdnimg.cn/img_convert/260a2283a2b15fbdfa6da27a11145bed.gif)](https://blog.csdn.net/swazer_z/article/details/72902721 "复制代码")
xf.empty.request=true}

null

[外链图片转存中…(img-I9oRA3NY-1652164522666)]

[外链图片转存中…(img-J8vrU6Xw-1652164522666)]

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/874515.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号