• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

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

 
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

   
 
Marshal
Posts: 3149
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • 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
Marshal
Posts: 3149
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • 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
Marshal
Posts: 3149
466
Android Eclipse IDE TypeScript Redhat MicroProfile Quarkus Java Linux
  • 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.
 
    Bookmark Topic Watch Topic
  • New Topic