Java Spring Rest和Swagger

huangapple 未分类评论59阅读模式
英文:

Java Spring Rest and Swagger

问题

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

在Swagger的定义中,我发现应该创建两个模型:Odd(对象)和Bet(数组)。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部分,不包括代码。如果您有其他问题,请随时提问。

英文:

I faced the problem related to Swagger and Java. My lecturer sent me a Swagger file from which I should create a REST API. Also, that REST API should export the same Swagger documentation as Lecturers.

In the Swagger definitions I found that there should be created 2 Models: Odd(object) and Bet(array). Everything is fine with the Odd Model, but I do not find a solution on how to create Bet array. If I simply create an ArrayList named Bet in the getOdd method and put all Odd objects inside, the model will not be created.

I was looking for solutions, but I did not succeed. Thank you in advance.

Lecturer Swagger file:

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

I will paste some of my work done:

How my Models looks like in Swagger

How my getOdd method looks like in 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;
	 
}

答案1

得分: 0

你可以使用注解来控制Swagger定义的生成。有一个旧的和一个新的API来实现这一点:

在讲座的Swagger文件中,使用了swagger: "2.0",因此它是旧版本。新版本生成的Swagger文件适用于OpenAPI 3.0。

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

另请参阅JavaDoc文档:

英文:

You can use annotations to control the generation of the swagger definitions. There is a old and a new api to do that:

In the lecture swagger file 'swagger: "2.0"' is used. Therefore it would be the old one. The new one is producing swagger files for OpenApi 3.0.

Specially the annotation @ApiOperation and @ApiModelOperation could be interesting for you to solve your problem.

See also the JavaDoc:

huangapple
  • 本文由 发表于 2020年3月15日 08:06:07
  • 转载请务必保留本文链接:https://java.coder-hub.com/60688533.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定