会执行ContainerResponseFilter和Conta

会执行ContainerResponseFilter和Conta

本文介绍了球衣2.2:永远不会执行ContainerResponseFilter和ContainerRequestFilter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

遵循泽西岛网站上的入门指南:

Following the getting started guide on the Jersey website:

我执行了以下构建命令:

I executed the following build command:

$ mvn archetype:generate -DarchetypeArtifactId=jersey-quickstart-grizzly2 \
-DarchetypeGroupId=org.glassfish.jersey.archetypes -DinteractiveMode=false \
-DgroupId=com.example -DartifactId=simple-service -Dpackage=com.example \
-DarchetypeVersion=2.2

然后,我按照以下教程进行操作

I then followed the tutorial on

https://jersey.java.net/documentation/latest/filters-and -interceptors.html#d0e6783

添加自定义ContainerResponseFilter:

to add a custom ContainerResponseFilter:

@NameBinding
@Retention(RetentionPolicy.RUNTIME)
static @interface CORSBinding {}

@Provider
@Priority(Priorities.HEADER_DECORATOR)
@CORSBinding
static class CrossDomainFilter implements ContainerResponseFilter {
    @Override
    public void filter(ContainerRequestContext creq, ContainerResponseContext cres) {
        Logger.getLogger("com.example").log( Level.INFO, "before: {0}", cres.getHeaders());
        cres.getHeaders().add("Access-Control-Allow-Origin", "*");
        cres.getHeaders().add("Access-Control-Allow-Headers", "origin, content-type, accept, authorization");
        cres.getHeaders().add("Access-Control-Allow-Credentials", "true");
        cres.getHeaders().add("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD");
        cres.getHeaders().add("Access-Control-Max-Age", "1209600");
        Logger.getLogger("com.example").log( Level.INFO, "after: {0}", cres.getHeaders());
    }
}

@Provider
static class MyResponseFilter implements ContainerResponseFilter {

    @Override
    public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) throws IOException {
        System.out.println("MyResponseFilter.postFilter() enter");
        responseContext.setEntity(
                responseContext.getEntity() + ":" + getClass().getSimpleName(), null, MediaType.TEXT_PLAIN_TYPE);
        System.out.println("MyResponseFilter.postFilter() exit");
    }
}

...
@GET
@Produces(MediaType.TEXT_PLAIN)
@CORSBinding
public String helloWorld() {
    return "hello world";
}

我试图使用命名绑定和动态绑定注册此过滤器,但没有任何效果.

I tried to register this filter with Named Binding and with Dynamic Binding, nothing works.

为了易于复制,我还尝试了一个来自官方资源的示例:

To easily reproduce, I also tried an example from the official resources:

https://github.com/jersey/jersey/tree/2.2/examples/exception -映射

同样的问题:自定义过滤器无法执行.

The same problem: the custom filters do not get executed.

这是灰熊问题吗?

推荐答案

事实证明,您必须手动注册自定义类-如:

As it turns out you have to manually register the custom classes - as in:

rc.register(com.dummy.mypackage.CORSResponseFilter.class);

完整示例:

/**
 * Main class.
 *
 */
public class Main {
    // Base URI the Grizzly HTTP server will listen on
    public static final String BASE_URI = "http://192.168.1.34:8080/myapp/";

    /**
     * Starts Grizzly HTTP server exposing JAX-RS resources defined in this application.
     * @return Grizzly HTTP server.
     */
    public static HttpServer startServer() {
        // create a resource config that scans for JAX-RS resources and in com.example package
        final ResourceConfig rc = new ResourceConfig().packages("com.dummy.mypackage");

        //NEW: register custom ResponseFilter
        rc.register(com.dummy.mypackage.CORSResponseFilter.class);

        // Register Jackson JSON
        rc.packages("org.glassfish.jersey.examples.jackson").register(JacksonFeature.class);

        // create and start a new instance of grizzly http server
        // exposing the Jersey application at BASE_URI
        return GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI), rc);
    }
    ...
}

这篇关于球衣2.2:永远不会执行ContainerResponseFilter和ContainerRequestFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 19:35