Reputation: 91
I have generated the swagger.json from my jersey project and trying to test it locally on swagger editor but I am getting the well known error on the 'try it out' for each api : CORS header ‘Access-Control-Allow-Origin’ missing EVEN THOUGH I have added the required headers for each request:
java code
public Response getObject(@PathParam("name")String name) {
....
return Response.ok(myObect)
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Credentials", "true")
.header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, HEAD")
.header("Access-Control-Allow-Headers", "origin, content-type, accept, authorization").build();
}
generated swagger
'/object/{name}':
get:
consumes:
- application/json
produces:
- text/html
parameters:
- type: string
name: name
in: path
required: true
responses:
'200':
description: OK
headers:
Access-Control-Allow-Credentials:
type: string
Access-Control-Allow-Headers:
type: string
Access-Control-Allow-Methods:
type: string
Access-Control-Allow-Origin:
type: string
by the way, I am running the project on tomcat 8.5 and swagger on Wamp server, I hope that does not make any difference
Upvotes: 0
Views: 4516
Reputation: 61
If one uses the Spring framework, the easies way I found out to enable CORS is to put this annotation on top of your method (or whole controller class) - @CrossOrigin
It comes from org.springframework.web.bind.annotation.CrossOrigin
Once this is done, when the server is started you are able to use the "Try it out" via Swagger (OpenAPI) documentation and execute requests toward the server endpoints.
Upvotes: 0
Reputation: 574
I think this might be something to do with your server rather than the code itself!
wampmanager -> Apache -> Apache modules -> headers_module
Try to enable CORS on your WAMP server first.
Upvotes: 1