繁体   English   中英

Java Spring Rest 和 Swagger

[英]Java Spring Rest and Swagger

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

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

我正在寻找解决方案,但我没有成功。 先感谢您。

讲师 Swagger 文件:

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'**

模型在 Swagger 中的外观

在 Swagger 中 getOdd 方法应该是什么样子

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

我的模型在 Swagger 中的样子

我的 getOdd 方法在 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;

}

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

在讲座中使用了 swagger 文件 'swagger: "2.0"'。 因此,它将是旧的。 新的正在为 OpenApi 3.0 生成 swagger 文件。

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

另请参阅 JavaDoc:

  • @ApiOperation: https ://docs.swagger.io/swagger-core/v1.5.X/apidocs/index.html ? io/swagger/annotations/ApiOperation.html
  • @ApiModelProperty: https ://docs.swagger.io/swagger-core/v1.5.X/apidocs/index.html ? io/swagger/annotations/ApiModelProperty.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM