我已经从我的球衣项目中生成了swagger.json,并尝试在swagger编辑器上本地测试它,但是我在每个api的'try it out'上得到了众所周知的错误:CORS标题'Access-Control-Allow-Origin'丢失即使我已经为每个请求添加了所需的标头:
java代码
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();
}
生成的招摇
'/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
顺便说一句,我在tomcat 8.5上运行项目并在Wamp服务器上招摇,我希望这没有任何区别
我认为这可能与您的服务器有关,而不是代码本身!
wampmanager -> Apache -> Apache modules -> headers_module
尝试首先在WAMP服务器上启用CORS。