在 jersey 2.x 过滤器实现不起作用?

In jersey 2.x filter implementation does not work?

虽然我试图通过实施 ContainerResponseFilter.but 添加响应 header 但它不起作用。

Java代码:-

public class PoweredByResponseFilter implements ContainerResponseFilter {

@Override
public void filter(ContainerRequestContext requestContext,
        ContainerResponseContext responseContext) throws IOException {
    responseContext.getHeaders().add("X-Powered-By", "Jersey :-)");
}

 }

部署描述符:-

<?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Filters</display-name>
<servlet>
    <servlet-name>Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>org.jersey</param-value>
    </init-param>
    <init-param>
        <param-name>jersey.config.server.provider.classes</param-name>
        <param-value>org.jersey.filter.PoweredByResponseFilter,org.jersey.filter.AuthorizationRequestFilter</param-value>
    </init-param>

</servlet>
<servlet-mapping>
    <servlet-name>Application</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>login.html</welcome-file>
</welcome-file-list>
 </web-app>

回应header:-

新描述符:-

i change my descriptor to                                                                            <servlet>
    <servlet-name>Application</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.packages</param-name>
        <param-value>org.jersey</param-value>
    </init-param>

</servlet>
<servlet-mapping>
    <servlet-name>Application</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>        

你的初始参数名称有误。这个

jersey.config.server.provider.classes

应该是

jersey.config.server.provider.classnames

here

您可以做的另一件事是简单地用 @Provider 注释过滤器 class。只要class在jersey.config.server.provider.packages列出的包(或子包)中,就会被提取并自动注册。

例如,如果过滤器用 @Provider 注释,它在 org.jerseyorg.jersey.filtersorg.jersey.foo.filters 中,默认情况下它将被拾取并注册。这样您就不需要在 xml 配置中明确列出它。