繁体   English   中英

HTTP请求拦截器-使用CXF的Restful Web服务

[英]http request interceptor - restful web service using cxf

尝试向我的宁静服务收到的每个请求添加标头。 下面的入站http拦截器将被调用,但不会添加标头。

    package com.client.interceptors;

    import java.util.Collections;
    import java.util.List;
    import java.util.Map;

    import org.apache.cxf.interceptor.Fault;
    import org.apache.cxf.message.Message;
    import org.apache.cxf.phase.AbstractPhaseInterceptor;
    import org.apache.cxf.phase.Phase;

    public class ClientInterceptor extends AbstractPhaseInterceptor<Message> {
        public ClientInterceptor() {
            super(Phase.PRE_INVOKE); // Put this interceptor in this phase
            System.out.println("inside constructor");
        }

        public void handleMessage(Message msg) throws Fault {
            // process the message

            System.out.println("inside interceptor");
            Map<String, List> headers = (Map<String, List>) msg
            .get(Message.PROTOCOL_HEADERS);

            headers.put("token",
            Collections.singletonList("abcd1234xyz56sa"));

            msg.put(Message.PROTOCOL_HEADERS, headers);

        }
    }

如何做到这一点?

好吧,我有一个相同的解决方案。 看看这个。 它可能会有所帮助。

Client client = Client.create();
     WebResource webResource =client.resource("URL");

     MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
     queryParams.add("json", js); //set parametes for request

     appKey = "Addkey " + appKey; // appKey is unique number

     //Get response from RESTful Server get(ClientResponse.class);
     ClientResponse response = null;
     response = webResource.queryParams(queryParams)
                            .header("Content-Type", "application/json;charset=UTF-8")
                            .header("Authorization", appKey)
                            .get(ClientResponse.class);

     String jsonStr = response.getEntity(String.class);

暂无
暂无

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

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