Apache CXF - 在In和Out拦截器之间共享数据 [英] Apache CXF - share data between In and Out interceptors

查看:116
本文介绍了Apache CXF - 在In和Out拦截器之间共享数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Apach CXF作为REST提供程序。

I am using Apach CXF as REST provider.

我想在进入webservice时收集数据,在输入resposne之前收集数据并添加一些计算响应。对于这个问题而言,为了简单起见,我们假设我想获得进入时的开始时间,发送响应之前的结束时间,并将总时间添加到响应中。

I want to gather data when I enter the webservice, gather data before I enter the resposne and add some calculation to the response. For this question and for simplicity, lets assume I want to get the starting time on entering, the finishing time before the response is sent and add the total time to the response.

现在,我该怎么做?我创建了In和Out拦截器单独工作,但我如何使用Out拦截器中In拦截器的数据?

Now, how do I do that? I created In and Out interceptors that works fine alone, but how do I use the data from the In interceptor in the Out interceptor?

谢谢
Idob

Thanks Idob





我尝试将数据设置为上下文参数

I tried to set the data as contextual parameter with

message.setContextualProperty(key,value);

但我在

message.getContextualProperty(key);

我也尝试了相同但只是

message.put(key,value) and message.get(key)

不起作用。

想法是谁?

谢谢你,
Idob

Thank you, Idob

推荐答案

您可以在 交换 。 CXF为每个请求创建一个 Exchange 对象,作为请求/响应对的输入和输出消息的容器,并使其可以作为消息访问来自两者的.getExchange()

You can store values on the Exchange. CXF creates an Exchange object for each request to act as a container for the in and out messages for the request/response pair and makes it accessible as message.getExchange() from both.

在拦截器中:

public void handleMessage(Message inMessage) throws Fault {
  inMessage.getExchange().put("com.example.myKey", myCustomObject);
}

Out interceptor

Out interceptor

public void handleMessage(Message outMessage) throws Fault {
  MyCustomObject obj = (MyCustomObject)outMessage.getExchange().get("com.example.myKey");
}

(反之亦然,客户端拦截器,其中存储将存储值和in将检索它们)。选择一个你知道其他拦截器不会使用的密钥 - 一个包合格的名称是一个不错的选择。请注意,如消息 Exchange StringMap 和有一个通用的put / get方法,它带有一个 Class 作为为你提供编译时类型安全的密钥,并且保存你不得不进行强制转换:

(or vice-versa for client-side interceptors, where the out would store values and the in would retrieve them). Choose a key that you know won't be used by other interceptors - a package-qualified name is a good choice. Note that, like Message, Exchange is a StringMap and has generic put/get methods taking a Class as the key that give you compile-time type safety and save you having to cast:

theExchange.put(MyCustomObject.class, new MyCustomObject());
MyCustomObject myObj = theExchange.get(MyCustomObject.class);

这篇关于Apache CXF - 在In和Out拦截器之间共享数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆