[英]Cannot access services after adding corsfilter in resteasy
I cannot access any of my services after adding corsfilter to my ApplicationPath 将corsfilter添加到ApplicationPath后,我无法访问任何服务
this is the example 这是一个例子
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import org.jboss.resteasy.plugins.interceptors.CorsFilter;
@ApplicationPath("/")
public class JaxRsActivator extends Application {
private Set<Object> singletons = new HashSet<Object>();
private Set<Class<?>> classes = new HashSet<Class<?>>();
public JaxRsActivator() {
CorsFilter corsFilter = new CorsFilter();
corsFilter.getAllowedOrigins().add("*");
corsFilter.setAllowedHeaders("Content-Type");
singletons.add(corsFilter);
}
@Override
public Set<Class<?>> getClasses() {
return classes;
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
If I do a curl I get this, 如果我卷曲,我会得到这个,
curl -i http://localhost:8080/unika/usuarios -H "Origin:otherdomain.com"
HTTP/1.1 404 Not Found
Connection: keep-alive
Access-Control-Allow-Origin: otherdomain.com
X-Powered-By: Undertow 1
Server: Wildfly 8
Access-Control-Allow-Credentials: true
Content-Length: 0
Date: Tue, 16 Jun 2015 16:18:15 GMT
On the server I get this 在服务器上我得到了这个
11:18:15,533 WARN [org.jboss.resteasy.core.ExceptionHandler] (default task-10) failed to execute: javax.ws.rs.NotFoundException: Could not find resource for full path: http://localhost:8080/unika/usuarios at org.jboss.resteasy.core.registry.ClassNode.match(ClassNode.java:73) [resteasy-jaxrs-3.0.10.Final.jar:] 11:18:15,533 WARN [org.jboss.resteasy.core.ExceptionHandler](默认任务-10)无法执行:javax.ws.rs.NotFoundException:无法找到完整路径的资源: http:// localhost:8080 / unika / usuarios at org.jboss.resteasy.core.registry.ClassNode.match(ClassNode.java:73)[resteasy-jaxrs-3.0.10.Final.jar:]
If I take this lines off the code: 如果我从代码中删除这一行:
CorsFilter corsFilter = new CorsFilter();
corsFilter.getAllowedOrigins().add("*");
corsFilter.setAllowedHeaders("Content-Type");
singletons.add(corsFilter);
After commenting those lines I get the following with curl 在评论这些行后,我得到以下卷曲
$ curl -i http://localhost:8080/unika/usuarios -H "Origin:otherdomain.com"
HTTP/1.1 200 OK
Connection: keep-alive
Cache-Control: no-cache
X-Powered-By: Undertow 1
Server: Wildfly 8
Transfer-Encoding: chunked
Content-Type: application/json
Date: Tue, 16 Jun 2015 16:21:09 GMT
["Bill Burke","Stian Thorgersen","Stan Silvert","Gabriel Cardoso","Viliam Rockai","Marek Posolda","Bol"]
But I need Access-Control-Allow-Origin 但我需要Access-Control-Allow-Origin
You have to manually update the classes of your rest services like 您必须手动更新其他服务的类,例如
classes.add(MyRestService.class);
and this line 这条线
corsFilter.setAllowedHeaders("Content-Type");
gave me trouble access-Control-Allow-Headers, so I took that line an everything worked. 给了我访问控制 - 允许 - 标题的麻烦,所以我把那一行都搞定了。
A JAX-RS application can be configured with no web.xml and an empty Application
subclass with @ApplicationPath
. 可以使用
@ApplicationPath
配置JAX-RS应用程序,而不使用web.xml和空的Application
子类。 With this class, this is enough for your JAX-RS application to be bootstrapped, and the JAX-RS runtime will scan the classpath for all classes annotated with @Path
and @Provider
, and automatically register those classes with the application. 使用此类,这足以使您的JAX-RS应用程序被引导,并且JAX-RS运行时将扫描类路径以查找使用
@Path
和@Provider
注释的所有类,并自动向应用程序注册这些类。
@ApplicationPath("/api")
public class JaxRsApplication extends Application {}
Once you override the getSingeletons()
or getClasses()
and return a non-empty set in either of them, you disable the automatic registration of classes through classpath scanning. 一旦覆盖了
getSingeletons()
或getClasses()
并在其中任何一个中返回非空集,就会通过类路径扫描禁用类的自动注册。 Since you have done so, your resources are no longer automatically registered. 由于您已经这样做,您的资源不再自动注册。 So now you can do a couple things:
所以现在你可以做几件事:
Just register the resources class(es) that was before automatically registered 只需注册自动注册之前的资源类
classes.add(MyResource.class);
You can have a look at this answer , which uses a Feature
annotated with @Provider
. 您可以查看此答案 , 该答案使用使用
@Provider
注释的Feature
。 The Feature
will get registered because of the classpath scanning. 由于类路径扫描,
Feature
将被注册。 In this feature is where you can register the CorsFilter
with the FeatureContext
. 在此功能中,您可以使用
FeatureContext
注册CorsFilter
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.