【问题标题】:Sending Response from Spring WS Interceptor从 Spring WS 拦截器发送响应
【发布时间】:2013-10-19 13:45:44
【问题描述】:

根据文档,我需要在拦截器的 handleRequest(..) 方法中返回 false。但是我的问题是如何使用 jaxb2 设置特定响应?我知道应该使用messageContext.setResponse(..) 方法。但是这里不知道如何将jaxb2请求转换为messageContext接受的WebServiceMessage。

为了详细说明我的问题,我将使用前面在 Stack Over Flow 上提出的一些相关问题的示例,可以在 here 找到。

从给定的示例场景中说,假设我想从拦截器发送已处理的订单响应以获取特定订单 ID?

@Override
  public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception {

    boolean proceed = true;

    SaajSoapMessage saajSoapMessage = (SaajSoapMessage) messageContext.getRequest();


    SoapBody requestBody = saajSoapMessage.getSoapBody();
    Object obj = jaxb2Marshaller.unmarshal(requestBody.getPayloadSource());


    if (obj instanceof ConfirmOrderRequest ) {
      ConfirmOrderRequest cor = (ConfirmOrderRequest ) obj;

      String orderId = cor.getOrderId();

      ...........
      .......
    }
  .....
 }

假设我的 jaxb2 响应类是 ConfirmOrderResponse。

【问题讨论】:

    标签: spring-ws


    【解决方案1】:

    这基本上与阅读相反(虽然我没有测试过这段代码):

    @Inject 
    SaajSoapMessageFactory mf;
    
    ... 
    
    SaajSoapMessage message = mf.createWebServiceMessage();
    messageContext.setResponse(message);
    MarshallingUtils.marshal(jaxbMarshaller, cor, message)
    

    在实际端点而不是在拦截器中执行此操作会容易得多。

    【讨论】:

    • 测试过,效果很好,这里我用的是@Autowired,而不是@Inject。其实我是用拦截器来识别重复请求的,所以拦截器是比端点更好的选择。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多