似乎 CXF 必须使用 addHeader 而不是 setHeader 来替换 Jetty 已经插入的值。
最后,我不得不编写一个 ServletFilter 和 ResponseWrapper,它们只在 /api 上下文中使用。该线程最终使我沿着这条路线前进。
DateHeaderStrippingFilter.java
public class DateHeaderStrippingFilter
implements Filter
{
private HashSet<String> headers;
@Override
public void destroy()
{
headers = null;
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException
{
HeaderStripperWrapper wrappedResponse = new HeaderStripperWrapper((HttpServletResponse)response, headers);
chain.doFilter(request, wrappedResponse);
}
@Override
public void init(FilterConfig config)
throws ServletException
{
headers = new HashSet<String>();
headers.add("Date");
}
}
HeaderStripperWrapper.java
public class HeaderStripperWrapper
extends HttpServletResponseWrapper
{
private HashSet<String> headersToStrip;
public HeaderStripperWrapper(HttpServletResponse response, HashSet<String> headersToStrip)
{
super(response);
this.headersToStrip = headersToStrip;
}
@Override
public void addHeader(String name, String value)
{
if (!headersToStrip.contains(name))
{
super.addHeader(name, value);
}
}
@Override
public void setHeader(String name, String value)
{
if (!headersToStrip.contains(name))
{
super.setHeader(name, value);
}
}
}
然后在配置嵌入式码头服务器时
Context api = new Context(contexts, "/api", false, false);
BooksApiServlet apiServlet = env.getInjector().getInstance(ApiServlet.class);
ServletHolder holder = new ServletHolder(apiServlet);
api.addServlet(holder, "/*");
api.addFilter(DateHeaderStrippingFilter.class, "/*", 0);