尝试将过滤器添加到Grizzly + Jersey应用的问题

编程入门 行业动态 更新时间:2024-10-28 00:25:51
本文介绍了尝试将过滤器添加到Grizzly + Jersey应用的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

I have this server initialization class:

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.URI; public class Main { public static final String BASE_URI = "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 */ } }

以及这个过滤器类,我试图在初始化类中注册:

And this filter class which I'm trying to register in the initialization class:

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; } }

当我运行我得到的应用程序这段日志:

When I run the app I get this piece of log:

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构建文件:

I'm working with gradle and this is my app's Gradle build file:

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' }

推荐答案

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

You're mixing 2 versions of Jersey (1.x and 2.x) in your app. In your Main class you're using Jersey 2.x (package prefix is org.glassfish.jersey) and your ContainerResponseFilter is an implementation of Jersey 1.x proprietary API.

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

If you want to use Jersey 2.x the filter should implement ContainerResponseFilter from JAX-RS 2.0.

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

In case you want to stick with Jersey 1.x you should change registration in your Main class (and also use grizzly container module from Jersey 1.x):

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

更多推荐

尝试将过滤器添加到Grizzly + Jersey应用的问题

本文发布于:2023-11-09 17:22:57,感谢您对本站的认可!
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:过滤器   Grizzly   Jersey

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!