Java 问题 Resteasy 3.09 CorsFilter

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29388937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:55:11  来源:igfitidea点击:

Problems Resteasy 3.09 CorsFilter

javajax-rscorsresteasy

提问by puzste

I tried to use the new CorsFilterwhich 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

我尝试使用CorsFilterResteasy 3.0.9 中提供的新功能。我在本页底部找到了一个示例: Ajax request with JAX-RS/RESTEasy implementation CORS

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

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

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

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

在以下页面上,我找到了一个描述: javax.ws.rs.NotFoundException:找不到完整路径的资源错误发生

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).

但基本上,此部署选项的作用是扫描应用程序的@Path、@Provider 等注释。原因是 JAX-RS 将首先分别在重写的 getClasses() 和 getSingletons() 中查找类和对象。如果然后返回空集,这会告诉 JAX-RS 进行扫描(根据规范)。

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

因此,如果我覆盖该getSingletons()方法,JAX-RS 不会进行扫描?还有另一种方法来配置它CorsFilter并启用资源扫描吗?

采纳答案by Paul Samsotha

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

“还有其他方法可以配置这个 CorsFilter 并启用资源扫描吗?”

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

保持扫描的一种方法是实现一个 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.

此功能将像所有其他@Providers 和@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!

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!