gpt4 book ai didi

java - 在 Jersey 2.x 中过滤器实现不起作用?

转载 作者:行者123 更新时间:2023-11-30 03:16:23 24 4
gpt4 key购买 nike

虽然我尝试通过实现 ContainerResponseFilter 添加响应 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 :-

enter image description here

新描述符:-

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>

最佳答案

您的 init-param 名称错误。这个

jersey.config.server.provider.classes

应该是

jersey.config.server.provider.classnames

参见here .

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

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

关于java - 在 Jersey 2.x 中过滤器实现不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32480729/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com