JAX-RS客户端过滤器,用于在将请求发送到服务器之前修改标题 [英] JAX-RS Client Filter to Modify Header Before Request is Dispatched to server

查看:117
本文介绍了JAX-RS客户端过滤器,用于在将请求发送到服务器之前修改标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JAX-RS(RestEasy)中,我想实现一个客户端过滤器,该过滤器在发送请求之前会修改标头,因此我不会为每个调用手动进行此操作.

In JAX-RS (RestEasy), I want to implement a client filter that modifies the header before sending the request so I don't do this manually for every single call.

目前,我正在接收端执行此操作,以在到达资源之前拦截请求.

Currently I'm doing this in the receiving end to intercept requests before arriving to the resource.

@Provider
@Priority(Priorities.AUTHENTICATION)
public class AuthenticationFilter implements ContainerRequestFilter {

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {

        // read header
    }

现在我知道了(如果我错了,请纠正我):

Now I know this (Correct me if I'm wrong):

在接收端,可以在请求到达资源并获取请求之前使用ContainerRequestFilter.

In the receiving end, ContainerRequestFilter can be used before the request arrives to the resource and get the request.

但是我想在客户端实现此功能,以便在将请求发送到服务器之前修改标头.可以使用相同的服务器过滤器,还是可以使用类似于客户端的过滤器?

But I want to implement this in the client side, to modify the header before the request is ever sent to the server. Can the same server filter be used or there is something similar to for the client?

推荐答案

您必须注册客户

Client client = ClientBuilder.newClient().register(MyFilter.class);

@Provider
public class MyFilter implements ClientRequestFilter {

    @Override
    public void filter(ClientRequestContext ctx) throws IOException {
        // modify header before send: ctx.getHeaders() 
    }
}  

这篇关于JAX-RS客户端过滤器,用于在将请求发送到服务器之前修改标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆