[英]Apache CXF: get SOAP message inside service method
我們正在使用Apache CXF托管Web服務。 我有一個基本問題
是否有可能在實際的Web服務方法中獲取肥皂消息/有效載荷? 該Web服務會將傳遞給它的值保存在數據庫中,與此同時,我們希望將實際的XML請求/響應(有效負載)保存在數據庫中。
我已經嘗試過處理程序/攔截器,在那里我可以看到SOAP消息。 但是我想要Webservice方法中的XML有效負載,以便可以執行必要的操作並將有效負載保存到數據庫。
自問這個問題以來已經很長時間了,但萬一有人遇到同樣的問題:
這個想法是使用spring上下文,因為它比攔截器上下文大
您需要以編程方式將XML請求注冊為spring bean,然后將其通過Web服務方法獲取
PS:攔截器和Web服務實現者都必須實現ApplicaionContextAware接口
這是我所做的,並且對我有用:
在您的攔截器中:
@Component
public class XmlInInterceptor extends AbstractPhaseInterceptor<Message> implements ApplicationContextAware{
private static final String BEAN_NAME = "yourBeanName";
private ConfigurableApplicationContext context;
private DefaultSingletonBeanRegistry registry;
public XmlInInterceptor() {
super(Phase.PRE_PROTOCOL);
}
@Override
public void handleMessage(final Message message) throws Fault {
try {
// get the SOAP message as String
final SOAPMessage soapMessage = message.getContent(SOAPMessage.class);
final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
soapMessage.writeTo(byteArrayOutputStream);
final String encoding = (String) message.get(Message.ENCODING);
final String xmlRequest = new String(byteArrayOutputStream.toByteArray(), encoding);
//here is the fun part: here where you need to register the bean in runtime
SingletonBeanRegistry registry = context.getBeanFactory();
// you must check if the bean is already registered and destroy it
// otherwise the registerSingleton will fail
if(registry.containsSingleton(BEAN_NAME)){
registry.destroySingleton(BEAN_NAME);
}
registry.registerSingleton(BEAN_NAME, xmlRequest);
} catch (final Exception e) {
logger.error(e.getMessage(), e);
throw new Fault(e);
}
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
context = (ConfigurableApplicationContext) applicationContext;
registry = (DefaultSingletonBeanRegistry) context.getBeanFactory();
}
在您的Web服務方法中:
public class MyWebServiceImpl implements MyWebService, ApplicationContextAware {
@Override
public Response myWebMethod(MyParams params) {
//get the XML request as classic spring bean access
final String xmlRequesteXML = applicationContext.getBean("xmlRequest", String.class);
//custom code here
}
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.applicationContext = applicationContext;
}
}
我認為這是不可能的。 但是您可以使用aspectj來捕獲此方法的調用,然后獲得SOAP消息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.