提问者:小点点

Spring REST问候服务的简单招摇留档


我使用Spring REST服务实现来学习Swagger。

GET上的服务-http://localhost:8080/greeting?name=Betlista返回

{
  "id":5,
   "content":"Hello, Betlista!"
}

所以我为Swagger创建了YAML文件:

swagger: "2.0"
info:
  version: "0.0.1-SNAPSHOT"
  title: "Spring REST"
host: "localhost:8080"
basePath: "/"
tags:
- name: "Greeting"
schemes:
- "http"
paths:
  /greeting:
    get:
      operationId: "greeting"
      produces:
      - "application/json"
      parameters:
      - name: "name"
        in: "query"
        required: false
        type: "string"
      responses:
        "200":
          description: "successful operation"
          schema:
            $ref: "#/definitions/GreetingResponse"
definitions:
  GreetingResponse:
    type: "object"
    properties:
      id:
        type: "integer"
      content:
        type: "string"

问题是,当我尝试使用“试用”按钮执行它时。

似乎(从开发人员工具中Chrome的网络选项卡)没有响应:

…而浏览器中的常规调用工作正常

编辑:正如我在评论中提到的,我验证了swagger生成的curl,它按预期工作-curl-X GET"http://localhost:8080/greeting?name=Betlista"-H"接受:application/json"

目前我正在研究CORS话题(这对我来说毫无意义,因为留档和服务都在localhost),我在Chrome控制台看到:


共1个答案

匿名用户

这实际上是服务的问题,而不是斯威格编辑器的问题。

我仍在寻找解决方案,此处描述:

CORS使用特殊的HTTP标头来允许跨域请求。“试用”功能需要在API响应中使用以下标头:

Access-Control-Allow-Origin: https://host.from.which.the.request.came
Vary: Origin
Access-Control-Allow-Credentials: true
Access-Control-Expose-Headers: ResponseHeader1, ResponseHeader2, ...

我尝试将Spring REST服务与CORS一起使用,在curl-Ihttp://localhost:8080/greeting?name=Betlista返回:

HTTP/1.1 200
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Content-Type: application/json
Content-Length: 37
Date: Mon, 02 Nov 2020 13:42:15 GMT

但这仍然不起作用…

正如我所说,这不是斯威格编辑器的问题,而是服务问题。

CORS检查可以在启动它Chrome关闭chrome. exe--disable-web-security--user-data-dir=/tmp,当然不推荐,所以我认为这只是一个解决方法。

我最后用的是这个-https://stackoverflow.com/a/47022289/384674

编辑:后来我用-https://stackoverflow.com/a/40300363/384674