[英]Overwrite HTTP Header in CXF (JAX-RS)
I would like to change the value of the HTTP Header "Connection" before sending a reply from the Server to the Client. 我想在从服务器向客户端发送答复之前更改HTTP标头“连接”的值。
My Use Case: I have a JAX-RS Web Service that sits behind a Load Balancer. 我的用例:我有一个JAX-RS Web服务,位于负载均衡器后面。 The Web Service Client sends requests with "Connection: Keep-Alive". Web Service客户端通过“连接:保持活动”发送请求。 Result: The connection is kept open, the Load Balancer does not balance :-) 结果:连接保持打开状态,负载均衡器不平衡 :-)
So I would like my Web Service to reply every few hundred requests with a "Connection: close" to force the Client to open a new connection. 因此,我希望Web服务使用“连接:关闭”来每隔几百个请求进行回复,以强制客户端打开新的连接。
How can I do this with CXF? 如何使用CXF做到这一点?
You can use a ContainerResponseFilter
to add your desired header to the response sent. 您可以使用ContainerResponseFilter
将所需的标头添加到发送的响应中。
An example: 一个例子:
import java.io.IOException;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
@Provider
public class ResponseFilter implements ContainerResponseFilter {
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext)
throws IOException {
MultivaluedMap<String, Object> headers = responseContext.getHeaders();
headers.putSingle("Connection", "close");
}
}
Declare this class as a Provider in your javax.ws.rs.core.Application
. 在您的javax.ws.rs.core.Application
中将此类声明为Provider。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.