简体   繁体   中英

Cannot access services after adding corsfilter in resteasy

I cannot access any of my services after adding corsfilter to my 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:]

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

Update

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

@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. Since you have done so, your resources are no longer automatically registered. So now you can do a couple things:

  1. Just register the resources class(es) that was before automatically registered

     classes.add(MyResource.class); 
  2. You can have a look at this answer , which uses a Feature annotated with @Provider . The Feature will get registered because of the classpath scanning. In this feature is where you can register the CorsFilter with the FeatureContext .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM