如何从JAX-WS中的Spring Aspect中访问SOAPMessage?

时间:2012-02-20 17:32:05

标签: java web-services spring jax-ws

我有一个使用Spring 3 IOC的JAX-WS WebService。我编写了一个Spring Aspect来处理任何异常,并确保在完成调用之前在WebService类中正确处理它们。在我的Aspect中,我想访问用户的语言环境(在我的SOAP Header中定义),但是我不知道如何做到这一点。

我知道我可以在处理程序中获取Locale,但这对我的方面没有帮助。我尝试注入WebServiceContext,但它始终为null。

一点点挖掘指向https://issues.apache.org/jira/browse/CXF-2674,这似乎表明:

  1. Spring故意不注入WebServiceContext对象
  2. WebServiceContext是SOAPMessageContext
  3. 的瘦包装器

    但是,如果我尝试自动装配SOAPMessageContext,那么也会失败,并显示以下错误消息:

    Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private javax.xml.ws.handler.soap.SOAPMessageContext com.cws.cs.lendingsimulationservice.error.ServiceErrorInterceptor.webServiceContext; nested exception is java.lang.IllegalArgumentException: Can not set javax.xml.ws.handler.soap.SOAPMessageContext field com.cws.cs.lendingsimulationservice.error.ServiceErrorInterceptor.webServiceContext to java.util.LinkedHashMap
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:502) [spring-beans-3.0.5.RELEASE.jar:]
            at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:84) [spring-beans-3.0.5.RELEASE.jar:]
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:282) [spring-beans-3.0.5.RELEASE.jar:]
            ... 21 more
    Caused by: java.lang.IllegalArgumentException: Can not set javax.xml.ws.handler.soap.SOAPMessageContext field com.cws.cs.lendingsimulationservice.error.ServiceErrorInterceptor.webServiceContext to java.util.LinkedHashMap
            at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:146) [:1.6.0_29]
            at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(UnsafeFieldAccessorImpl.java:150) [:1.6.0_29]
            at sun.reflect.UnsafeObjectFieldAccessorImpl.set(UnsafeObjectFieldAccessorImpl.java:63) [:1.6.0_29]
            at java.lang.reflect.Field.set(Field.java:657) [:1.6.0_29]
            at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:498) [spring-beans-3.0.5.RELEASE.jar:]
            ... 23 more
    

    我认为必须有一种方法可以从一个方面访问这些信息,但似乎无法弄清楚如何。

    如果我尝试注入SOAPMessage对象,则会收到org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [javax.xml.soap.SOAPMessage] found for dependency错误消息。

    有人能指出我正确的方向吗?

    谢谢,

    埃里克

1 个答案:

答案 0 :(得分:2)

我的解决方案使用处理程序和请求范围的对象。希望其他人可能会发现这在将来很有用

处理程序:

public class ServiceContextHandler implements SOAPHandler<SOAPMessageContext>{

    /**
     * Logger
     */
    private static final Logger logger = LoggerFactory.getLogger(ServiceContextHandler.class);

    /**
     * Request object
     */
    @Autowired
    private SOAPHeaderData soapHeaderData;

    /**
     * Ensure that the bean has its parameters injected appropriately
     */
    @PostConstruct
    public void init() {
        SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this);
    }

    /**
     * Nothing to do on end of message
     */
    public void close(MessageContext context) {
    }


    /**
     * Nothing to do for a fault
     */
    public boolean handleFault(SOAPMessageContext context) {
        return true;
    }

    /**
     * Process the message
     */
    public boolean handleMessage(SOAPMessageContext context) {
        // no need to bother with outbound requests
        if( !((Boolean) context.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY)).booleanValue() )
            process(context);
        return true;
    }

    /**
     * Doesn't handle any specific QNames in the header
     */
    public Set<QName> getHeaders() {
        return null;
    }


    /**
     * Extract the header parameters
     * @param context
     */
    @SuppressWarnings("unchecked")
    private void process(SOAPMessageContext context) {
        // Creating the XML tree
        try {
            JAXBContext jc = JAXBContext.newInstance( ObjectFactory.class);
            Object[] headers = context.getHeaders(new ObjectFactory().createServiceContext(null).getName(), jc, true);

            // find the service context element
            for( Object header : headers ){
                if( (header instanceof JAXBElement<?>) && ((JAXBElement<?>)header).getValue() instanceof ServiceContextType){
                    // found the service context element
                    soapHeaderData.setServiceContext(((JAXBElement<ServiceContextType>)header).getValue());
                    break;
                }
            }

        } catch (JAXBException e) {
            logger.error(ExceptionUtils.getStackTrace(e));
        } catch (WebServiceException e) {
            logger.error(ExceptionUtils.getStackTrace(e));
        }
    }
}

SOAPHeaderData的定义:

<!-- SOAPHeaderData -->
<bean id="soapHeaderData" class="com.cws.cs.lendingsimulationservice.header.SOAPHeaderDataImpl" scope="request">
    <aop:scoped-proxy proxy-target-class="false"/>
</bean>

其中SOAPHeaderDataImpl是一个基本的POJO(带有接口以便不需要CGLIB)。

希望这有帮助!如果有什么不清楚,请告诉我。

谢谢,

埃里克