Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How can I get past CORS preflight request while using JAX-RS for standalone Jetty server?

 
Geralld Murphy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to get a standalone Jetty server running with a web client. However I am running into issues with the preflight requests that are being sent before the actual request. When I make a request using Postman, I get the expected response (preflight request are not used in Postman), but when the request is made from Chrome I get the following error message:

XMLHttpRequest cannot load http://my.server.ip:8000/hello/api/test. Response to preflight request doesn't pass access control check: No
'Access-Control-Allow-Origin' header is present on the requested
resource. Origin 'http://localhost:8080' is therefore not allowed
access.


The header I get back is as follows:

HTTP/1.1 200 OK Date: Wed, 26 Oct 2016 14:51:06 GMT Allow:
POST,OPTIONS Date: Wed, 26 Oct 2016 14:51:06 GMT Content-Length: 0
Server: Jetty(9.2.10.v20150310)


My relevant code is shown below. Any help solving this would be greatly appreciated as I have already spent several days trying to figure this out. If there is a way to fix this while still using JAXRSServerFactoryBean that would be great.
Thanks.

My Standalone server class is below:

  

  
My webs services interface is below:

  

Here is my implementation:

  

And here is my web.xml

   
 
Ron McLeod
Bartender
Pie
Posts: 1048
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not familiar with Jetty, but I addressed a similar issue with Grizzly2 by adding a ContainerResponseFilter.

 
Geralld Murphy
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the response Ron. I have tried adding the ContainerResponseFilter as you suggested but am still getting the same error. Do I need to be calling or creating an instance of the AccessControlFilter somewhere in my server code or is the @Provider annotation supposed to take care of tying this class to the rest of my code? Thanks.  
 
Ron McLeod
Bartender
Pie
Posts: 1048
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure what version of CXF you are using, but it looks like CXF v2 does not support registering filters and other providers using the @Provider annotation, and that they need to be registered manaully: Apache CXF - Registering custom providers. CXF v3 does support the @Provider annotation.
 
Ron McLeod
Bartender
Pie
Posts: 1048
65
Android Eclipse IDE Java Linux MySQL Database Redhat
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if you're not committed to Apache CXF, you might want to give Jersey a look - it will happily work with Jetty server that you are using now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic