Trying to add headers to every request that my restful service receives. The below inbound http interceptor gets invoked, but does not add the header.
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);
}
}
How can this be achieved?
Well ya i have one solution for the same. look this. it might help.
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);
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.