java - SWAGGER 2 从同一基础对象继承请求和响应对象

标签 java spring spring-boot inheritance swagger-2.0

在使用 Swagger 2.0 设计的 Spring API 中,我尝试使用 swagger 创建继承。我想创建一个基础对象,它具有请求和响应对象的共同属性。我尝试像下面的例子那样做:

CategoryResponse:
  allOf:
    - $ref: '#/definitions/Category'
    - type: object
      properties:
        id:
          type: integer
          example: '1'          
CategoryRequest:
  type: object
  allOf:
    - $ref: '#/definitions/Category'         
Category:
  discriminator: nameCategory
  type: object
  properties:
    nameCategory:
      type: string
      example: Games

问题是我在尝试 POST 或 PUT 新的 CategoryRequest 对象时收到错误请求错误。它甚至没有到达 API Controller ,所以我猜问题可能出在上面的模型定义中。我尝试了很多变体,但没有一个有效。但是,当我尝试获取类别列表或按 id 获取一个类别时,我能够这样做(我的 CategoryResponse 正在工作并很好地扩展了类别)。

有谁知道使用通用基础模型的继承来创建此结构的正确方法(对于请求和响应对象)?

提前致谢!

最佳答案

id 看起来像一个自动生成的只读属性。在这种情况下,您不需要继承 - 您可以使用单个 Category 架构并将 id 标记为 readOnly: true

Category:
  type: object
  properties:
    nameCategory:
      type: string
      example: Games
    id:
      type: integer
      readOnly: true  # <-----
      example: 1

来自OpenAPI Specification :

readOnly

Declares the property as "read only". This means that it MAY be sent as part of a response but MUST NOT be sent as part of the request.

关于java - SWAGGER 2 从同一基础对象继承请求和响应对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53562078/

相关文章:

java - 如何将文本应用到文本区域

java - 从 android.support.design.widget.FloatingActionButton 膨胀 float 操作按钮

java - 与CXF 2.4.10相关的Spring初始化失败

java - 如何使用 Spring Security 重新加载用户更新权限

java - 如何使用Spring Boot处理Rest API中的异常

java - 具有 MappedSuperclass 和子类的 Spring Data 存储库的 Spring Boot 自定义实现

java - Java 中的方法绑定(bind)

java - 从我的 WebView 中的相机或图库上传图像

java - 如何使用 Spring Boot 从 S3 下载 json 文件?

grails - 配置Spring Boot Security在Grails 3.0中使用BCrypt密码编码