Resteasy 3.09 CorsFilter 问题
Problems Resteasy 3.09 CorsFilter
我尝试使用 Resteasy 3.0.9 中提供的新 CorsFilter
。我在本页底部找到了一个示例:
Ajax request with JAX-RS/RESTEasy implementing CORS
如果我在方法 getSingletons()
(属于 Application
子类)中定义此过滤器,那么我的资源将不再被扫描。这意味着将找不到资源并出现以下错误:
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).
所以如果我覆盖 getSingletons()
方法,JAX-RS 不会进行扫描?是否有另一种配置此 CorsFilter
并启用资源扫描的方法?
"Is there another way to configure this CorsFilter and enable the resource scanning?"
保持扫描的一种方法是实施 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
和 @Path
一样扫描此功能。
仅使用
进行测试
@ApplicationPath("/api")
public class RestApplication extends Application {
}
C:\>curl -i http://localhost:8080/api/simple -H "Origin:whosebug.com"
HTTP/1.1 200 OK
Date: Wed, 01 Apr 2015 12:07:22 GMT
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: whosebug.com
Content-Type: application/octet-stream
Content-Length: 15
Server: Jetty(9.2.4.v20141103)
Hello Response!
我尝试使用 Resteasy 3.0.9 中提供的新 CorsFilter
。我在本页底部找到了一个示例:
Ajax request with JAX-RS/RESTEasy implementing CORS
如果我在方法 getSingletons()
(属于 Application
子类)中定义此过滤器,那么我的资源将不再被扫描。这意味着将找不到资源并出现以下错误:
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).
所以如果我覆盖 getSingletons()
方法,JAX-RS 不会进行扫描?是否有另一种配置此 CorsFilter
并启用资源扫描的方法?
"Is there another way to configure this CorsFilter and enable the resource scanning?"
保持扫描的一种方法是实施 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
和 @Path
一样扫描此功能。
仅使用
进行测试@ApplicationPath("/api")
public class RestApplication extends Application {
}
C:\>curl -i http://localhost:8080/api/simple -H "Origin:whosebug.com" HTTP/1.1 200 OK Date: Wed, 01 Apr 2015 12:07:22 GMT Access-Control-Allow-Credentials: true Access-Control-Allow-Origin: whosebug.com Content-Type: application/octet-stream Content-Length: 15 Server: Jetty(9.2.4.v20141103)
Hello Response!