在 RESTEasy JAX-RS 中注入自定义响应标头

发布于 2024-10-26 05:14:19 字数 378 浏览 8 评论 0原文

我有 RESTEasy (JAX-RS) 服务器,大约有 60 个服务(到目前为止)。我想使用服务器构建时间自动向提供程序调用者注入自定义响应标头:X-BuildTime: 20100335.1130

有没有一种简单的方法可以在不修改我的每项服务的情况下做到这一点?

我正在尝试使用一个类来实现带有注释@Provider@ServerInterceptororg.jboss.resteasy.spi.interception.PostProcessInterceptor,但是我不知道如何修改传递到我的 postProcess() 方法中的 ServerResponse。

I have RESTEasy (JAX-RS) server with about 60 services (so far). I would like to automatically inject a custom response header to provider callers with the server build time: X-BuildTime: 20100335.1130.

Is there an easy way to do this without modifying each of my services?

I am trying to use a class that implements org.jboss.resteasy.spi.interception.PostProcessInterceptor with annotations @Provider and @ServerInterceptor, but I can't figure out how to modify the ServerResponse that is passed into my postProcess() method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

烟若柳尘 2024-11-02 05:14:19

虽然 MessageBodyWriterInterceptor 可以解决这个问题,但最好使用 PostProcessInterceptor,因为它会拦截不调用 MessageBodyWriters 的响应(例如 Response.created(URI.create) (“/rest/justcreated”)).build())。

有关详细信息,请参阅官方文档

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.annotations.interception.ServerInterceptor;
import org.jboss.resteasy.core.ServerResponse;
import org.jboss.resteasy.spi.interception.PostProcessInterceptor;

@Provider
@ServerInterceptor
public class MyPostProcessInterceptor implements PostProcessInterceptor {

    @Override
    public void postProcess(ServerResponse response) {
        MultivaluedMap<String, Object> headers = response.getMetadata();
        List<Object> domains = headers.get("X-BuildTime");
        if (domains == null) { domains = new ArrayList<Object>(); }
        domains.add("20100335.1130");
        headers.put("X-BuildTime", domains);
    }

}

Although MessageBodyWriterInterceptor does the trick, it is better to use PostProcessInterceptor, as it will intercept responses that do not call MessageBodyWriters (such as Response.created(URI.create("/rest/justcreated")).build()).

For more info, see the official documentation.

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;

import org.jboss.resteasy.annotations.interception.ServerInterceptor;
import org.jboss.resteasy.core.ServerResponse;
import org.jboss.resteasy.spi.interception.PostProcessInterceptor;

@Provider
@ServerInterceptor
public class MyPostProcessInterceptor implements PostProcessInterceptor {

    @Override
    public void postProcess(ServerResponse response) {
        MultivaluedMap<String, Object> headers = response.getMetadata();
        List<Object> domains = headers.get("X-BuildTime");
        if (domains == null) { domains = new ArrayList<Object>(); }
        domains.add("20100335.1130");
        headers.put("X-BuildTime", domains);
    }

}
变身佩奇 2024-11-02 05:14:19

我认为使用 javax.servlet.Filter 将是一个更简单的解决方案:

public void doFilter ( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException {
   HttpServletResponse httpResponse = (HttpServletResponse)response;
   httpResponse.setHeadder(header, headerValue);
   chain.doFilter(request, response);
}

在 web.xml 中为相关 url 配置它,然后就完成了。

I think using javax.servlet.Filter will be a much easier solution:

public void doFilter ( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException {
   HttpServletResponse httpResponse = (HttpServletResponse)response;
   httpResponse.setHeadder(header, headerValue);
   chain.doFilter(request, response);
}

configure it in web.xml for the relevant urls, and you are done.

永不分离 2024-11-02 05:14:19

使用 javax.ws.rs.core.Response 怎么样;这样您就可以在创建响应数据的同一位置设置标头。

@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public Response test(           ){
    HashMap<String,String> ret = new HashMap<String,String>();
    ret.put("foo","bar");
    return Response
       .status(Response.Status.OK)
       .entity(ret)
       .header("X-say", "Hello world!")
       .build();
}

How about using javax.ws.rs.core.Response ; this way you can set the header in the same place where you create the response-data.

@GET
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public Response test(           ){
    HashMap<String,String> ret = new HashMap<String,String>();
    ret.put("foo","bar");
    return Response
       .status(Response.Status.OK)
       .entity(ret)
       .header("X-say", "Hello world!")
       .build();
}
昵称有卵用 2024-11-02 05:14:19

您还可以通过 MessageBodyInterceptors

(查看第 30.1 节末尾的示例)

@Provider
@ServerInterceptor
public class MyHeaderDecorator implements MessageBodyWriterInterceptor {

    public void write(MessageBodyWriterContext context) throws IOException, WebApplicationException
    {
       context.getHeaders().add("My-Header", "custom");
       context.proceed();
    }
}

You can also change header by MessageBodyInterceptors

( check the example at the end of section 30.1 )

@Provider
@ServerInterceptor
public class MyHeaderDecorator implements MessageBodyWriterInterceptor {

    public void write(MessageBodyWriterContext context) throws IOException, WebApplicationException
    {
       context.getHeaders().add("My-Header", "custom");
       context.proceed();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文