ContainerRequestFilter
interface. Most of all, we can easily adapt or remove this feature in the Web configuration file.
Here’s an example to resolve Access-Control-Origin not allowed error and you can set those parameters want you want:
src/main/java/com/example/filter/CORSFilter.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | |
The next step is to define your web.xml
like so for filtering the response:
src/main/webapp/WEB-INF/web.xml
1 2 3 4 5 6 7 8 9 10 11 | |
However, the ajax cross-domain request may accompany with another OPTIONS
request, we can observe it in the Tomcat localhost_access log as following:
10.4.128.61 - - [06/Aug/2013:00:00:21 +0800] "OPTIONS /example/rest/logs HTTP/1.0" 200 520
10.4.128.61 - - [06/Aug/2013:00:00:22 +0800] "POST /example/rest/logs HTTP/1.0" 200 34
“preflighted” requests first send an HTTP OPTIONS request header to the resource on the other domain, in order to determine whether the actual request is safe to send.
MOZILLA DEVELOPER NETWORK HTTP Access Control (CORS)
Also the catalina.out
throws the message as:
Aug 07, 2013 7:22:55 AM com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypes
INFO: Couldn't find grammar element for class java.io.InputStream
Aug 07, 2013 7:22:55 AM com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypes
INFO: Couldn't find grammar element for class javax.ws.rs.core.Response
I add the corresponding OPTIONS handler for all the @Path resource to solve the errors, however, I have no idea whether there is a better way to do this?
1 2 3 4 5 6 7 8 9 10 | |