簡體   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