简体   繁体   English

如何将相同的标头/ cookie添加到所有JAX-RS响应中?

[英]How can I add the same header/cookie to all JAX-RS responses?

I want to add cookies to all JAX-RS responses produced in the application. 我想向应用程序中生成的所有JAX-RS响应添加cookie。 How can I do this? 我怎样才能做到这一点? I don't use Response class. 我不使用Response类。

you can use interceptors: http://cxf.apache.org/docs/jax-rs.html#JAX-RS-Filters%2CInterceptorsandInvokers 您可以使用拦截器: http : //cxf.apache.org/docs/jax-rs.html#JAX-RS-Filters%2CInterceptorsandInvokers

probably you will be needed to write your own (extend it from some existing default output interceptor) if there is no ready-for-use solution 如果没有现成的解决方案,可能需要您编写自己的文件(从某些现有的默认输出拦截器扩展它)

You can use a HTTPFilter that adds the cookie value to your requests. 您可以使用HTTPFilter将Cookie值添加到您的请求中。 If not that then you can get hold of the context in your methods and manually handle the cookie handling 如果不是这样,那么您可以掌握方法中的上下文并手动处理cookie处理

public Response updateCustomer(@Context HttpHeaders h, Customer c) {
   ...
}

Jersey has a concept of container filters you can use for this. 泽西岛有一个可以用于此目的的容器过滤器概念。 In this particular case you can implement a ContainerResponseFilter that adds the cookie to the header. 在这种特殊情况下,您可以实现一个ContainerResponseFilter,将Cookie添加到标头中。 See the documentation on how to create and register container filters here: http://jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/container/filter/package-summary.html 在此处查看有关如何创建和注册容器过滤器的文档: http : //jersey.java.net/nonav/apidocs/latest/jersey/com/sun/jersey/api/container/filter/package-summary.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM