问题Resteasy 3.09 CorsFilter [英] Problems Resteasy 3.09 CorsFilter

查看:524
本文介绍了问题Resteasy 3.09 CorsFilter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用Resteasy 3.0.9中提供的新 CorsFilter 。我在本页底部找到了一个示例:
Ajax请使用JAX-RS / RESTEasy实现CORS



如果我在方法 getSingletons()中定义此过滤器 Application 子类),则我的资源不再被扫描。这意味着没有资源将被找到,并发生以下错误:


javax.ws.rs.NotFoundException:找不到完整路径的资源错误发生


在下一页我发现了一个描述:
javax.ws。 rs.NotFoundException:无法找到完整路径的资源错误发生



正在扫描应用程序的@Path,@Provider等注释。原因是JAX-RS将首先分别在重载的getClasses()和getSingletons()中查找类和对象。


因此,JAX- RS不做扫描,如果我覆盖 getSingletons()方法?是否有另一种方法配置此 CorsFilter 并启用资源扫描?

解决方案


是否有另一种方法来配置此CorsFilter并启用资源扫描?


保持扫描的方式只是实施 javax.ws.rs.core.Feature

  import javax.ws.rs .core.Feature; 
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.Provider;
import org.jboss.resteasy.plugins.interceptors.CorsFilter;

@Provider
public class CorsFeature implements Feature {

@Override
public boolean configure(FeatureContext context){
CorsFilter corsFilter = new CorsFilter();
corsFilter.getAllowedOrigins()。add(*);
context.register(corsFilter);
return true;
}
}

此功能将像所有其他 @Provider s和 @Path s。



只有

  @ApplicationPath(/ api)
public class RestApplication extends Application {
}




C:\> curl -i http :// localhost:8080 / api / simple -H原产地:stackoverflow.com
HTTP / 1.1 200 OK
日期:Wed,01 Apr 2015 12:07:22 GMT
访问-Control-Allow-Credentials:true
Access-Control-Allow-Origin:stackoverflow.com
Content-Type:application / octet-stream
Content-Length:15
:Jetty(9.2.4.v20141103)



您好回应!



I tried to use the new CorsFilter which is available in Resteasy 3.0.9. I found an example at the bottom of this page: Ajax request with JAX-RS/RESTEasy implementing CORS

If I define this filter in the method getSingletons() (of the Application subclass) , then my Resources don't get scanned anymore. That means that no Resources will be found and the following error occurs:

javax.ws.rs.NotFoundException: Could not find resource for full path Error Occures

On the following page i found a description: javax.ws.rs.NotFoundException: Could not find resource for full path Error Occures

But basically, what this deployment option does is scan for annotations of @Path, @Provider, etc for the application. The reason is that JAX-RS will first look for classes and object in overridden getClasses() and getSingletons(), respectively. If then return empty sets, this tell JAX-RS to do scanning (per the spec).

So JAX-RS doesn't do a scanning if i overwrite the getSingletons() method? Is there another way to configure this CorsFilter and enable the resource scanning`?

解决方案

"Is there another way to configure this CorsFilter and enable the resource scanning?"

One way to keep the scanning is just to implement a javax.ws.rs.core.Feature

import javax.ws.rs.core.Feature;
import javax.ws.rs.core.FeatureContext;
import javax.ws.rs.ext.Provider;
import org.jboss.resteasy.plugins.interceptors.CorsFilter;

@Provider
public class CorsFeature implements Feature {

    @Override
    public boolean configure(FeatureContext context) {
        CorsFilter corsFilter = new CorsFilter();
        corsFilter.getAllowedOrigins().add("*");
        context.register(corsFilter);
        return true;
    }  
}

This feature will get scanned for just like all other @Providers and @Paths.

Test with only

@ApplicationPath("/api")
public class RestApplication extends Application {
}

C:\>curl -i http://localhost:8080/api/simple -H "Origin:stackoverflow.com" HTTP/1.1 200 OK Date: Wed, 01 Apr 2015 12:07:22 GMT Access-Control-Allow-Credentials: true Access-Control-Allow-Origin: stackoverflow.com Content-Type: application/octet-stream Content-Length: 15 Server: Jetty(9.2.4.v20141103)

Hello Response!

这篇关于问题Resteasy 3.09 CorsFilter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆