【发布时间】: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;
}
【问题讨论】: