java - http 请求拦截器 - 使用 cxf 的 Restful Web 服务

标签 java rest cxf

尝试向我的 Restful 服务收到的每个请求添加 header 。下面的入站 http 拦截器被调用,但不添加 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);

        }
    }

如何实现这一目标?

最佳答案

嗯,我有一个解决方案。看看这个。这可能会有所帮助。

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);

关于java - http 请求拦截器 - 使用 cxf 的 Restful Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25967073/

相关文章:

java - Apache CXF JAXB Marshaller 未正确编码 @XmlAttribute

java - 无法激活 CXF ResponseTimeFeature

java - 如何使用spring boot admin 2版本和spring boot 1.5版本?

java - 将整数转换为 boolean 值选择位数

javascript - 将 JSON 发布到 WCF REST 端点

java - 使用 RolesAllowed 的 Jersey 声明链接权限

cxf - 在 apache cxf 中使用 jax-rs 自定义日期格式?

java - 重新设置构建路径后不接受 Iterator<String>

java - 如何从sun bug数据库中的内部版本号查找jdk版本

ruby-on-rails - Rails 2.3.9 缺少 POST 负载