如何插入SOAPHandler(JAX-WS)?

时间:2012-05-18 09:56:31

标签: web-services jax-ws webservice-client jax-ws-customization

我想用SOAPHandler处理SOAP头,即我创建了SOAPHandler,但是如何将它插入指定的服务?换句话说,这个处理程序应该处理这个服务的消息..可能我需要使用一些注释或xml配置...?..

1 个答案:

答案 0 :(得分:0)

这可以通过以下步骤完成。

<强> 1。定义处理程序

public class CalculatorSOAPHandlerOne implements SOAPHandler<SOAPMessageContext> {

    private static final Logger logger = LoggerFactory.getLogger(CalculatorSOAPHandlerOne.class);

    @Override
    public Set<QName> getHeaders() {
        return null;
    }

    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        if(!(Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)){
            logger.info(" soap message passed through CalculatorSOAPHandlerOne (only for request)");
        }
        return true;
    }

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        return true;
    }

    @Override
    public void close(MessageContext context) {

    }
}

<强> 2。在XML中声明处理程序链声明    (处理程序-chain.xml)

  <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<javaee:handler-chains
        xmlns:javaee="http://java.sun.com/xml/ns/javaee"
        xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <javaee:handler-chain>
        <javaee:handler>
            <javaee:handler-class>com.chathurangaonline.jaxws.samples.handler.CalculatorSOAPHandlerOne</javaee:handler-class>
        </javaee:handler>

        <javaee:handler>
    </javaee:handler-chain>
</javaee:handler-chains>

第3。为JAX-WS服务实现添加处理程序链

@WebService
@HandlerChain(file = "handler-chain.xml")
public class CalculatorService{

    private  static final Logger logger = LoggerFactory.getLogger(CalculatorServiceImpl.class);

    @Override
    public double add(double num1, double num2) {
        logger.info("== calling add method ==");
        return num1 + num2;
    }

    @Override
    public double multiply(double num1, double num2) {
        return num1 * num2;
    }
}