cf8 发表于 2017-9-17 10:48

CXF拦截器获取调用方法名

//拦截器
package me.yourname.webservice.cxfinterceptor;

import java.lang.reflect.Method;
import org.apache.cxf.frontend.MethodDispatcher;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;
import org.apache.cxf.service.Service;
import org.apache.cxf.service.model.BindingOperationInfo;
public class MethodToInvokeInterceptor extends AbstractPhaseInterceptor<Message> {
    //至少要一个带参的构造函数
    public MethodToInvokeInterceptor(String phase) {
      super(phase);
    }
    public MethodToInvokeInterceptor() {
                super(Phase.USER_LOGICAL);
        }
    public void handleMessage(Message message) throws Fault {
            Exchange exchange = message.getExchange();
                BindingOperationInfo bop = exchange.get(BindingOperationInfo.class);
                MethodDispatcher md = (MethodDispatcher) exchange.get(Service.class)
                                .get(MethodDispatcher.class.getName());
                Method method = md.getMethod(bop);
                System.out.println("********method name:" + method.getName());
    }
}
//handler
package me.yourname.webservice.handler;

import java.lang.reflect.Method;

import javax.xml.ws.handler.Handler;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.handler.soap.SOAPMessageContext;

import org.apache.cxf.frontend.MethodDispatcher;
import org.apache.cxf.jaxws.context.WrappedMessageContext;
import org.apache.cxf.message.Exchange;
import org.apache.cxf.message.Message;
import org.apache.cxf.service.Service;
import org.apache.cxf.service.model.BindingOperationInfo;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
@Component("methodToInvokeHandler")
public class MethodToInvokeHandler implements Handler<SOAPMessageContext> {

        private static Logger log = Logger.getLogger(MethodToInvokeHandler.class);

        public void close(MessageContext messageContext) {
                log.info("MethodToInvokeHandler close");
        }

        public boolean handleFault(SOAPMessageContext messageContext) {
                log.error("MethodToInvokeHandler error");
                return false;
        }

        public boolean handleMessage(SOAPMessageContext messageContext) {
                WrappedMessageContext wmc = (WrappedMessageContext) messageContext;
                Message m = wmc.getWrappedMessage();
                Exchange exchange = m.getExchange();
                BindingOperationInfo bop = exchange.get(BindingOperationInfo.class);
                MethodDispatcher md = (MethodDispatcher) exchange.get(Service.class)
                                .get(MethodDispatcher.class.getName());
                Method method = md.getMethod(bop);
                System.out.println("invoke method:"+method.getName());
                return true;
        }
}

fjhSIRU15 发表于 2017-9-17 12:24

没毛病啊
页: [1]
查看完整版本: CXF拦截器获取调用方法名