简体   繁体   English

Java Spring Rest 和 Swagger

[英]Java Spring Rest and Swagger

I faced the problem related to Swagger and Java.我遇到了与 Swagger 和 Java 相关的问题。 My lecturer sent me a Swagger file from which I should create a REST API.我的讲师给我发送了一个 Swagger 文件,我应该从中创建一个 REST API。 Also, that REST API should export the same Swagger documentation as Lecturers.此外,该 REST API 应该导出与讲师相同的 Swagger 文档。

In the Swagger definitions I found that there should be created 2 Models: Odd(object) and Bet(array).在 Swagger 定义中,我发现应该创建 2 个模型:Odd(object) 和 Bet(array)。 Everything is fine with the Odd Model, but I do not find a solution on how to create Bet array.使用 Odd 模型一切正常,但我没有找到有关如何创建 Bet 数组的解决方案。 If I simply create an ArrayList named Bet in the getOdd method and put all Odd objects inside, the model will not be created.如果我只是在 getOdd 方法中创建一个名为 Bet 的 ArrayList 并将所有 Odd 对象放入其中,则不会创建模型。

I was looking for solutions, but I did not succeed.我正在寻找解决方案,但我没有成功。 Thank you in advance.先感谢您。

Lecturer Swagger file:讲师 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'**

How Models should look like in Swagger模型在 Swagger 中的外观

How getOdd method should look like in Swagger在 Swagger 中 getOdd 方法应该是什么样子

I will paste some of my work done:我将粘贴我完成的一些工作:

How my Models looks like in Swagger我的模型在 Swagger 中的样子

How my getOdd method looks like in Swagger我的 getOdd 方法在 Swagger 中的样子

My Rest Controller:我的休息控制器:

@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;
}

My Odd class:我的奇数班级:

@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;

}

You can use annotations to control the generation of the swagger definitions.您可以使用注释来控制 swagger 定义的生成。 There is a old and a new api to do that:有一个旧的和一个新的 api 来做到这一点:

In the lecture swagger file 'swagger: "2.0"' is used.在讲座中使用了 swagger 文件 'swagger: "2.0"'。 Therefore it would be the old one.因此,它将是旧的。 The new one is producing swagger files for OpenApi 3.0.新的正在为 OpenApi 3.0 生成 swagger 文件。

Specially the annotation @ApiOperation and @ApiModelOperation could be interesting for you to solve your problem.特别是注释@ApiOperation@ApiModelOperation可能对您解决问题很有趣。

See also the JavaDoc:另请参阅 JavaDoc:

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

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