【问题标题】:Problems trying to add a filter to a Grizzly+Jersey app尝试向 Grizzly+Jersey 应用程序添加过滤器时出现问题
【发布时间】:2013-11-12 03:04:23
【问题描述】:

我有这个服务器初始化类:

package magic.app.main;

import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;

import java.io.IOException;
import java.net.URI;

public class Main {
    public static final String BASE_URI = "http://localhost:4747/";

    public static void main(String[] args) throws IOException {
        /* SOME NON-RELEVANT CODE UP HERE */

        final ResourceConfig rc = new ResourceConfig();

        rc.packages("magic.app.resource");
        rc.register(magic.app.main.CorsSupportFilter.class);

        final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI),rc);

        /* SOME NON-RELEVANT CODE DOWN HERE */
    }
}

还有我试图在初始化类中注册的这个过滤器类:

package magic.app.main;

import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;

import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;

public class CorsSupportFilter implements ContainerResponseFilter {

    @Override
    public ContainerResponse filter(ContainerRequest req, ContainerResponse contResp) {

        ResponseBuilder resp = Response.fromResponse(contResp.getResponse());
        resp.header("Access-Control-Allow-Origin", Configuration.getHttpAllowOrigin())
            .header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");

        String reqHead = req.getHeaderValue("Access-Control-Request-Headers");

        if(null != reqHead && !reqHead.equals(null)){
            resp.header("Access-Control-Allow-Headers", reqHead);
        }

        contResp.setResponse(resp.build());
        return contResp;
    }

}

当我运行应用程序时,我会得到以下日志:

WARNING: A provider magic.app.main.CorsSupportFilter registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider magic.app.main.CorsSupportFilter will be ignored.

我正在使用 gradle,这是我应用的 Gradle 构建文件:

apply plugin: 'java'

repositories{
    mavenCentral()
}

dependencies{
    // IP2C
    compile fileTree(dir: 'libs', include: '*.jar')

    // Jersey + Grizzly
    compile 'org.glassfish.jersey:jersey-bom:2.4.1'
    compile 'org.glassfish.jersey.containers:jersey-container-grizzly2-http:2.4.1'

    // Jersey
    compile 'com.sun.jersey:jersey-core:1.17.1'
    compile 'com.sun.jersey:jersey-server:1.17.1'

    // JSON
    compile 'org.codehaus.jackson:jackson-core-asl:1.9.13'
    compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
    compile 'org.codehaus.jackson:jackson-xc:1.9.13'
    compile 'com.googlecode.json-simple:json-simple:1.1.1'

    // Jersey + JSON
    compile 'com.sun.jersey:jersey-json:1.17.1'
    compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.3.0-rc1'

    // Postgres
    compile 'com.jolbox:bonecp:0.8.0.RELEASE'
    compile 'postgresql:postgresql:9.1-901-1.jdbc4'

    // Mail
    compile 'javax.mail:mail:1.5.0-b01'
}

【问题讨论】:

    标签: java http header jersey grizzly


    【解决方案1】:

    您在应用中混合了 Jersey 的 2 个版本(1.x 和 2.x)。在您的 Main 类中,您使用的是 Jersey 2.x(包前缀为 org.glassfish.jersey),而您的 ContainerResponseFilter 是 Jersey 1.x 专有 API 的实现。

    如果你想使用 Jersey 2.x,过滤器应该从 JAX-RS 2.0 实现 ContainerResponseFilter

    如果您想坚持使用 Jersey 1.x,您应该在 Main 类中更改注册(并使用 Jersey 1.x 中的 grizzly 容器模块):

    final ResourceConfig rc = new PackagesResourceConfig("magic.app.resource");
    rc.getContainerResponseFilters().add(CorsSupportFilter.class);
    

    【讨论】:

    • ¡¡¡谢谢!!!!我知道这与我的依赖关系有关。但我不知道 Glassfish 提供的是 Jersey v2 和 Sun v1,因此,我很困惑。一段时间以来,我一直在与此作斗争。在对 Jersey v1 vs v2 进行分析后,我决定使用 v2。稍后进行一些温和的重构,现在我的 API 再次作为独立应用程序工作。再次感谢您!。
    猜你喜欢
    • 2019-10-12
    • 2018-03-14
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 2022-01-09
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    相关资源
    最近更新 更多