【问题标题】:Java Spring Rest and SwaggerJava Spring Rest 和 Swagger
【发布时间】:2020-03-15 00:06:07
【问题描述】:

我遇到了与 Swagger 和 Java 相关的问题。我的讲师给我发了一个 Swagger 文件,我应该从中创建一个 REST API。此外,该 REST API 应该导出与 Lecturers 相同的 Swagger 文档。

在 Swagger 定义中,我发现应该创建 2 个模型:Odd(object) 和 Bet(array)。奇数模型一切都很好,但我没有找到关于如何创建 Bet 数组的解决方案。如果我只是在 getOdd 方法中创建一个名为 Bet 的 ArrayList 并将所有 Odd 对象放入其中,则不会创建模型。

我一直在寻找解决方案,但没有成功。提前谢谢你。

讲师招摇文件:

swagger: "2.0"
info:
  description: "Schema"
  version: "1.0.0"
  title: "API"
tags:
- name: "odds"
  description: "Offer and return Odds"
schemes:
- "http"
paths:
  /odds:
    post:
      tags:
      - "odds"
      summary: "Offer odds for a bet"
      consumes:
      - "application/json"
      produces:
      - "application/json"
      parameters:
      - in: "body"
        name: "body"
        description: "Odds that should be offered for a bet"
        required: true
        schema:
          $ref: "#/definitions/Odds"
      responses:
        201:
          description: "Odds have been created for bet"
        400:
          description: "Invalid format of Odds"
  /odds/{betId}:
    get:
      tags:
      - "odds"
      summary: "Find Odds by Bet ID"
      description: "Returns a list of odds for a given bet ID"
      produces:
      - "application/json"
      parameters:
      - name: "betId"
        in: "path"
        description: "ID of bet to return"
        required: true
        type: "integer"
        format: "int64"
      responses:
        200:
          description: "Odds are returned for bet ID"
          schema:
            $ref: "#/definitions/Bet"
        400:
          description: "Invalid Bet ID supplied"
        404:
          description: "Bet not found for given ID"
definitions:
  Odds:
    type: "object"
    properties:
      betId:
        type: "integer"
        format: "int64"
      userId:
        type: "string"
        description: "ID of user who is offering the odds"
      odds:
        type: "string"
        example: "1/10"
  **Bet:
    type: "array"
    items:
      $ref: '#/definitions/Odds'**

How Models should look like in Swagger

How getOdd method should look like in Swagger

我将粘贴我完成的一些工作:

How my Models looks like in Swagger

How my getOdd method looks like in Swagger

我的休息控制器:

@RestController
@RequestMapping("/api")
public class OddController {

@Autowired 
OddRepository oddRepository;

@GetMapping("/odds/{betId}")
public Optional<Odd> getOdd(@PathVariable Long betId) {
        Optional<Odd> theOdd=oddRepository.findById(betId);
    return theOdd;
}

@PostMapping("/odds")
public Odd addOdd(@RequestBody Odd odd) {
    odd.setBetId((long) 0);
    oddRepository.save(odd);
    return odd;
}

我的奇数班:

@Entity
@Table(name="odds")
@Data
public class Odd {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name="betid")
    private Long betId;

    @Column(name="userid")
    private String userId;

    @Column(name="odds")
    private String odds;

}

【问题讨论】:

    标签: java spring swagger


    【解决方案1】:

    您可以使用注释来控制 swagger 定义的生成。有一个旧的和一个新的 api 可以做到这一点:

    在演讲大摇大摆的文件中使用了'swagger:“2.0”'。因此,它将是旧的。新版本正在为 OpenApi 3.0 生成 swagger 文件。

    特别是注释@ApiOperation@ApiModelOperation 可能对您解决问题很有帮助。

    另见 JavaDoc:

    【讨论】:

      猜你喜欢
      • 2016-03-05
      • 2016-12-14
      • 1970-01-01
      • 1970-01-01
      • 2020-03-16
      • 1970-01-01
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多