SOAP自定义响应标头 - JAX-WS

时间:2018-03-09 17:38:51

标签: java soap jax-ws

在我的JAX-WS Web服务中,我需要自定义SOAP响应信封。

目前,它是这样的:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <methodResponse xmlns="http://interfaces.webservice.ucmdb.com">
         <methodReturn>202</methodReturn>
      </methodResponse >
   </soapenv:Body>
</soapenv:Envelope>

我需要它看起来像这样:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
     <soapenv:Header>
    <code>123</code>
   </soapenv:Header>   
   <soapenv:Body>
      <getCIResponse xmlns="http://interfaces.webservice.ucmdb.com">
         <getCIReturn>202</getCIReturn>
      </getCIResponse>
   </soapenv:Body>
</soapenv:Envelope>

那么,如何在JAX-WS肥皂信封中编写<soapenv:Header>部分?

这是我的WebMethod实现(目前非常简单):

@WebMethod
    public int  operation(@WebParam(name="username", header=false)String id) {
        return 202;
    }

1 个答案:

答案 0 :(得分:0)

1。)为了在JAX-WS中为SOAP消息添加标头,您可以实现 SOAPHandler 接口。这将使您可以访问SOAPMessage,并且可以使用 SAAJ api添加/更新SOAP标头。请记住,此处理程序是双向的。

public class Handler1 implements SOAPHandler<SOAPMessageContext>{

    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        boolean isOutbound = (boolean) context.get(SOAPMessageContext.MESSAGE_OUTBOUND_PROPERTY);

        if(isOutbound){
            SOAPMessage msg = context.getMessage();
            try {
                //Grab the header
                SOAPHeader header = msg.getSOAPHeader();

                //Add whatever QName you need
                header.addHeaderElement(new QName("code"));

                //Save changes.
                msg.saveChanges();

            } catch (SOAPException e) {
                e.printStackTrace();
            }
        }
        //True continue, false halt.
        return true;
    }

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

    @Override
    public void close(MessageContext context) {

    }

    @Override
    public Set<QName> getHeaders() {
        //Can also add a QName here.
        return null;
    }

}

2.。)为了注册Handler,您需要通过应保存在类路径中的xml处理程序配置文件添加处理程序。

<?xml version="1.0" encoding="UTF-8"?>
<javaee:handler-chains xmlns:javaee="http://java.sun.com/xml/ns/javaee">
    <javaee:handler-chain>
        <javaee:handler>
            <javaee:handler-class>com.calvinmmiller.service.Handler1</javaee:handler-class>
        </javaee:handler>
    </javaee:handler-chain>
</javaee:handler-chains>    

3。)可以使用以下注释将此文件添加到SEI(服务端点接口):

import javax.jws.HandlerChain;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;

@WebService
@HandlerChain( file = "handlerFile.xml")
public class Service {
    public int operation(String id){
        return 202;
    }

    public static void main(String[] args){
        Endpoint.publish("http://localhost:8080/soap", new Service());
    }
}