简体   繁体   中英

How to add header to soap fault without interceptor

I'm trying to find a way to add header to soap fault with out using interceptor. Is there any alternative solution.

Basically I've my soap request as follows.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://kp.web.com/schema/" xmlns:sch1="http://kp.web.com/Shared/schema/">
   <soapenv:Header>
      <sch:clientHeader>
         <sch1:consumerId>12</sch1:consumerId>
      </sch:clientHeader>
   </soapenv:Header>
   <soapenv:Body>
      <sch:addRequest>
         <sch:field1>-3</sch:field1>
         <sch:field2>-1</sch:field2>
      </sch:addRequest>
   </soapenv:Body>
</soapenv:Envelope>

Success message.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" >
   <soap:Header>
      <serverHeader xmlns:ns2="http://kp.web.com/schema/" xmlns="http://kp.web.com/Shared/schema/">
         <ns2:consumerId>12</ns2:consumerId>
         <ns2:completionCode>100</ns2:completionCode>
      </serverHeader>
   </soap:Header>
   <soap:Body>
      <addResponse xmlns="http://kp.web.com/schema/" xmlns:ns2="http://kp.web.com/Shared/schema/">
         <result>-4</result>
      </addResponse>
   </soap:Body>
</soap:Envelope>

When fault occurs

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring>Faulted you sent -1 and -1</faultstring>
         <detail>
            <faultResponse xmlns:ns2="http://kp.web.com/schema/" xmlns="http://kp.web.com/Shared/schema/">400</faultResponse>
         </detail>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

I would expect fault response something like this.

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:sch="http://kp.web.com/schema/" xmlns:sch1="http://kp.web.com/Shared/schema/">
    <soap:Header>
          <serverHeader>
             <ns2:consumerId>12</ns2:consumerId>
             <ns2:completionCode>100</ns2:completionCode>
          </serverHeader>
   </soap:Header>
   <soap:Body>
      <soap:Fault>
         <faultcode>soap:Server</faultcode>
         <faultstring>Faulted you sent -1 and -1</faultstring>
         <detail>
             <faultResponse xmlns:ns2="http://kp.web.com/schema/" xmlns="http://kp.web.com/Shared/schema/">400</faultResponse>
         </detail>
      </soap:Fault>
   </soap:Body>
</soap:Envelope>

Can I use holder or is there any solution. I can't use interceptor because I need to send the consumer Id back. In interceptor I will not have request details.

You can use SOAP Message Handlers and Handler Chains . Another way do it use Filters . I use Handler Chains for modify SOAP header.

This link might be helpful.

You can still use an interceptor. There are two ways you can get the data you need from the interceptor:

1) In you business logic, if you have a WebServiceContext injected in (@Resource), you can set properties on that that you should be able to pick up later from the message. (or from the message.getExchange().getInMessage())

2) From the message, you can get the Exchange and then get the inMessage. From that message you can get the contents that were unmarshalled (inMessage.getContent(List.class)) and passed into your class.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM