私はこのサーバー初期化クラスを持っています:
package magic.app.main;
import org.glassfish.grizzly.http.server.HttpServer;
import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory;
import org.glassfish.jersey.server.ResourceConfig;
import java.io.IOException;
import java.net.URI;
public class Main {
public static final String BASE_URI = "http://localhost:4747/";
public static void main(String[] args) throws IOException {
/* SOME NON-RELEVANT CODE UP HERE */
final ResourceConfig rc = new ResourceConfig();
rc.packages("magic.app.resource");
rc.register(magic.app.main.CorsSupportFilter.class);
final HttpServer server = GrizzlyHttpServerFactory.createHttpServer(URI.create(BASE_URI),rc);
/* SOME NON-RELEVANT CODE DOWN HERE */
}
}
そして、初期化クラスに登録しようとしているこのフィルタークラス:
package magic.app.main;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
import com.sun.jersey.spi.container.ContainerRequest;
import com.sun.jersey.spi.container.ContainerResponse;
import com.sun.jersey.spi.container.ContainerResponseFilter;
public class CorsSupportFilter implements ContainerResponseFilter {
@Override
public ContainerResponse filter(ContainerRequest req, ContainerResponse contResp) {
ResponseBuilder resp = Response.fromResponse(contResp.getResponse());
resp.header("Access-Control-Allow-Origin", Configuration.getHttpAllowOrigin())
.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS");
String reqHead = req.getHeaderValue("Access-Control-Request-Headers");
if(null != reqHead && !reqHead.equals(null)){
resp.header("Access-Control-Allow-Headers", reqHead);
}
contResp.setResponse(resp.build());
return contResp;
}
}
アプリを実行すると、次のログが取得されます。
WARNING: A provider magic.app.main.CorsSupportFilter registered in SERVER runtime does not implement any provider interfaces applicable in the SERVER runtime. Due to constraint configuration problems the provider magic.app.main.CorsSupportFilter will be ignored.
私はgradleを使用しています。これは私のアプリのGradleビルドファイルです:
apply plugin: 'java'
repositories{
mavenCentral()
}
dependencies{
// IP2C
compile fileTree(dir: 'libs', include: '*.jar')
// Jersey + Grizzly
compile 'org.glassfish.jersey:jersey-bom:2.4.1'
compile 'org.glassfish.jersey.containers:jersey-container-grizzly2-http:2.4.1'
// Jersey
compile 'com.sun.jersey:jersey-core:1.17.1'
compile 'com.sun.jersey:jersey-server:1.17.1'
// JSON
compile 'org.codehaus.jackson:jackson-core-asl:1.9.13'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
compile 'org.codehaus.jackson:jackson-xc:1.9.13'
compile 'com.googlecode.json-simple:json-simple:1.1.1'
// Jersey + JSON
compile 'com.sun.jersey:jersey-json:1.17.1'
compile 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.3.0-rc1'
// Postgres
compile 'com.jolbox:bonecp:0.8.0.RELEASE'
compile 'postgresql:postgresql:9.1-901-1.jdbc4'
// Mail
compile 'javax.mail:mail:1.5.0-b01'
}