Problems Resteasy 3.09 CorsFilter

匿名 (未验证) 提交于 2019-12-03 01:31:01

问题:

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`?

回答1:

"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!



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!