虽然我试图通过实现ContainerResponseFilter来添加响应头,但它不起作用。
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>
响应标题: -
新描述符: -
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>
答案 0 :(得分:2)
您的init-param名称错误。此
return
应该是
jersey.config.server.provider.classes
请参阅here。
您可以做的另一件事就是使用jersey.config.server.provider.classnames
注释过滤器类。只要该类位于@Provider
中列出的包(或子包)中,它就会被选中并自动注册。
例如,如果过滤器使用jersey.config.server.provider.packages
注释,它位于@Provider
或org.jersey
或org.jersey.filters
中,则默认情况下会将其选中并注册。这样您就不需要在xml配置中明确列出它。