【问题标题】:Jersey Api not sending request through FilterJersey Api 未通过过滤器发送请求
【发布时间】:2020-05-15 04:00:05
【问题描述】:

接收请求时未应用 ContainerRequestFilter。

这是我的第一个 Stackoverflow 问题,所以我真的不知道要在我的问题中添加哪些细节。

这是我的过滤器

package tjo.tjaa;

@Provider
 class Securityfilter implements ContainerRequestFilter {


    private static final String AUTHORIZATION_HEADER="authorization";
    private static final String AUTHORIZATION_HEADER_PREFIX="Basic";
    private static final String SECURITY_URL="security";

    @Override
    public void filter(ContainerRequestContext requestContext) throws IOException {

        //this is where i want to execute code
        return;
 }

}

这是我的 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<!-- This web.xml file is not required when using Servlet 3.0 container,
     see implementation details http://jersey.java.net/nonav/documentation/latest/jax-rs.html -->
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Web 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>tjo.tjaa</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Web Application</servlet-name>
        <url-pattern>/webapi/*</url-pattern>
    </servlet-mapping>
</web-app>

【问题讨论】:

    标签: java rest filter


    【解决方案1】:

    我假设有多种方法可以将过滤器实现添加到 JAX-RS。添加自定义过滤器的一种方法是将它们添加到 Application 类实现中。在您的情况下,您将有一个 Application 类的实现,并在 getClasses API 下提供自定义过滤器:

    public class RestApplication extends Application {
    
      @Override
      public Set<Class<?>> getClasses() {
        return Sets.newHashSet(
            SecurityFilter.class);
      }
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-03-15
      • 1970-01-01
      • 2017-05-23
      • 1970-01-01
      • 2021-05-02
      • 2016-04-05
      • 2020-05-27
      • 2021-05-28
      相关资源
      最近更新 更多