没有在WAS自由上调用ContainerRequestFilter方法

时间:2016-10-21 10:27:00

标签: rest filter websphere-liberty

我的WAS Liberty服务器上有过滤器类:

Discount(int)

自定义注释:

// TestRequestFilter.java
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.ext.Provider;

@Secured
@Provider
@PreMatching
public class TestRequestFilter implements ContainerRequestFilter
{
    private final static Logger log = Logger.getLogger(DemoRESTRequestFilter.class.getName());

    @Override
    public void filter(ContainerRequestContext requestCtx) throws IOException
    {
        System.out.println("FILTER-REQUEST");
    }
}

休息一下:

@NameBinding
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.TYPE, ElementType.METHOD })
public @interface Secured {

}

Server.xml:

@GET
    @Consumes({ MediaType.APPLICATION_JSON })
    @Produces({ MediaType.APPLICATION_JSON })
    @Secured
    public Response login(@HeaderParam("Authorization") String authorization) {
        return Response.ok("OK").build();
    }

如何使过滤器在WAS上工作?

1 个答案:

答案 0 :(得分:0)

您需要注册TestRequestFilter

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

@ApplicationPath("/")
public class RestApplication extends Application
{
    @Override
    public Set<Object> getSingletons()
    {
        Set<Object> singletons = new HashSet<>();
        singletons.add(new TestRequestFilter());
        // register others, if any
        return singletons;
    }
}