簡體   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