gpt4 book ai didi

java - Resteasy 3.09 CorsFilter 问题

转载 作者:太空狗 更新时间:2023-10-29 22:44:02 27 4
gpt4 key购买 nike

我尝试使用 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

在下一页我找到了一段描述: 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: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!

关于java - Resteasy 3.09 CorsFilter 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29388937/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com