简体   繁体   English

如何从每个SOAP响应/请求添加/读取自定义标头抛出Java

[英]How to Add/Read Custom Header from each SOAP Response/Request throw Java

I have to add a custom Header into my SOAP Response and Read Header from SOAP Request what I did so far referring to this links link1 and link2 as follows 我必须在SOAP Request中添加一个自定义Header并从SOAP Request中读取Read Header我到目前为止所指的链接link1link2如下所示

Web Service Class: Web服务类:

import javax.jws.WebMethod;
import javax.jws.WebService;

@WebService()
@HandlerChain(name = "SoapHandler", file = "soaphandler.xml")
public class FooService {

    @WebMethod()
    public String sayHello(String name) {
        System.out.println("Hello: " + name);
        return "Hello " + name + "!";
    }
}

SOAP Handler Class: SOAP处理程序类:

package com.webservice;

import java.util.Set;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPHandler;
import javax.xml.ws.handler.soap.SOAPMessageContext;

public class SoapHandler implements SOAPHandler<SOAPMessageContext> {

    private static final Logger LOGGER = Logger.getLogger(SoapHandler.class.getName());

    @Override
    public void close(MessageContext arg0) {
        System.out.println("Colse Method");
        LOGGER.info("Close Method");
    }

    @Override
    public boolean handleFault(SOAPMessageContext arg0) {
        System.out.println("handleFault Method");
        LOGGER.info("handleFault Method");
        return false;
    }

    @Override
    public boolean handleMessage(SOAPMessageContext arg0) {
        System.out.println("handleMessage Method");
        LOGGER.info("handleMessage Method");
        return false;
    }

    @Override
    public Set<QName> getHeaders() {
        System.out.println("getHeaders Method");
        LOGGER.info("getHeaders Method");
        return null;
    }
}

Tester Class 测试员类

public class Tester {

    /**
     * @param args
     */
    public static void main(String[] args) {
        try {
            FooServiceServiceLocator locator = new FooServiceServiceLocator();
            FooService fooService = locator.getFooServicePort();
            System.out.println(fooService.sayHello("ashish"));
        } catch (ServiceException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (RemoteException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Handler Chain Configuration: 处理程序链配置:

<?xml version="1.0" encoding="UTF-8"?>
<jws:handler-chains xmlns:jws="http://java.sun.com/xml/ns/javaee">
  <jws:handler-chain>
    <jws:handler>
      <jws:handler-name>SoapHandler</jws:handler-name>
      <jws:handler-class>com.webservice.SoapHandler</jws:handler-class>
    </jws:handler>
  </jws:handler-chain>
</jws:handler-chains>

when I am calling this Tester class it gives me correct output as "Hello ashish!" 当我调用这个Tester类时,它给了我正确的输出“Hello ashish!” and my handleMessage(SOAPMessageContext arg0) method is getting executed when request comes in and goes out so how can I differentiate between Incoming Request and outgoing Response in my 并且我的handleMessage(SOAPMessageContext arg0)方法在请求进入和退出时执行,所以我如何区分传入请求和传出响应在我的

handleMessage(SOAPMessageContext arg0) method ? so that when request comes in I can read Header and when response goes out then I can Add my header into it Thanks....

use the context argument that you get in handleMessage 使用handleMessage中的context参数

arg0.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY);

and check the retunred boolean to identify the msg as request/response 并检查retunred布尔值以将msg标识为请求/响应

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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