我尝试从Resteasy(3.0.16)实施CorsFilter。我目前的环境是: java 7 和 jboss 7.1 。我尝试了几种方法,但没有人工作。
实际上CorsFeature.java(@Provider)它永远不会实例化,我无法获得正确的标题。我还尝试在Application类中使用覆盖方法(使用web.xml等),但CorsFeature从不拦截任何东西。
这是我的代码,如果有人可以帮助我:
的pom.xml
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.16.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.ws.rs</groupId>
<artifactId>javax.ws.rs-api</artifactId>
<version>2.0.1</version>
// I also tried put <scope>provided...
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
Application.java
@ApplicationPath("")
public class BaseApplication extends Application
{}
CorsFeature.java
@Provider
public class CorsFeature implements Feature {
public CorsFeature() {
System.out.println("never init..");
}
@Override
public boolean configure(FeatureContext context) {
CorsFilter corsFilter = new CorsFilter();
corsFilter.getAllowedOrigins().add("*");
context.register(corsFilter);
return true;
}
}
服务
@POST
@Path("/new")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response sendEmail() {
String value = "new";
return Response.status(200).entity(value).build();
}
标题回复:
Content-Length → 11
Content-Type → */*
Date → Sat, 09 Apr 2016 00:07:03 GMT
Server → Apache-Coyote/1.1
此致