- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有 swagger2 的 spring-boot 应用程序。我希望能够在 swagger 中将父对象列表映射到我的请求模型。我正在使用注释 atm,但也可以使用 yaml 文件。
假设我有一个抽象类 Person 和两个子类 Child 和 Adult。在我的请求中,我有一个 Person 列表,其中可以包含 Child 对象和 Adult 对象。
@JsonTypeInfo(
use = JsonTypeInfo.Id.NAME,
include = JsonTypeInfo.As.PROPERTY,
property = "type",
visible = true)
@JsonSubTypes({
@JsonSubTypes.Type(value = Child.class, name = "CHILD"),
@JsonSubTypes.Type(value = Adult.class, name = "ADULT")})
@ApiModel(value = "Child", subTypes = {Child.class, Adult.class}, discriminator = "type")
public abstract class Person {
@ApiModelProperty(notes = "Name of the person", example = "aaron")
private String name;
@ApiModelProperty(notes = "Birthdate of the person", example = "2000-07-10")
private Date birthDate;
@ApiModelProperty(notes = "Type of the person ('CHILD' or 'ADULT')", example = "CHILD")
private String type;
Child(String name, LocalDate birthdate) {
this.name = name;
this.birthdate = birthdate;
}
Child() {
}
}
public class Adult extends Person {
private String job;
public Adult(String name, Date birthdate, String job) {
super(name, birthdate);
this.job = job;
}
Adult() {
}
}
public class Child extends Person {
private List<String> toys;
public Child(String name, Date birthdate, List<String> toys) {
super(name, birthdate);
this.toys = toys;
}
Child() {
}
}
public class PersonRequest {
@ApiModelProperty(notes = "Year of insert", example = "2019")
private Integer year;
@ApiModelProperty(notes = "Month of insert", example = "1")
private Integer month;
@ApiModelProperty(notes = "List of persons")
private List<Person> persons;
public SimulationRequest(Integer year, Integer month, List<Person> persons) {
this.year = year;
this.month = month;
this.persons = persons;
}
private SimulationRequest() {
}
public Integer getYear() {
return year;
}
public Integer getMonth() {
return month;
}
public List<Person> getPersons() {
return persons;
}
}
PersonRequest {
persons (Array[Person], optional): List of persons ,
month (integer, optional): Month of insert ,
year (integer, optional): Year of insert
}Person {
name (string, optional): Birthdate of the person ,
birthDate (string, optional): Name of the person ,
type (string, optional): Type of the person ('CHILD' or 'ADULT')
}
PersonRequest {
persons (Array[Person], optional): List of persons ,
month (integer, optional): Month of insert ,
year (integer, optional): Year of insert
}Child {
name (string, optional): Birthdate of the person ,
birthDate (string, optional): Name of the person ,
type (string, optional): Type of the person ('CHILD' or 'ADULT')
toys (Array[string], optional): Toys of the child
}Adult {
name (string, optional): Birthdate of the person ,
birthDate (string, optional): Name of the person ,
type (string, optional): Type of the person ('CHILD' or 'ADULT')
job (string, optional): Job of the adult
}
{
"persons": [
{
"birthdate": "2000-07-10",
"name": "aaron",
"type": "CHILD",
"toys" : ["ball","lego"]
},
{
"birthdate": "1990-07-10",
"name": "sofia",
"type": "ADULT",
"job" : "developer"
}
],
"month": 6,
"year": 2019
}
最佳答案
这是我的示例,使用 Lombok 和 Swagger 3 注释。诀窍是在具有抽象类型的排序规则上使用“oneOf”或“anyOf”。见 https://swagger.io/docs/specification/describing-request-body/详情。
@Data
public class PersonRequest {
@Schema(description = "Either Child or Adult",
anyOf = {Child .class, Adult.class})
private List<? extends Person> persons;
}
public enum PersonType {
PARENT, CHILD
}
@Data
public abstract class Person {
@Schema(notes = "Name of the person", example = "aaron")
private String name;
@Schema(notes = "Birthdate of the person", example = "2000-07-10")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
private Date birthDate;
@Schema(notes = "Type of the person ('CHILD' or 'ADULT')", example = "CHILD")
private PersonType type; //this should be one of the enum values
Child(String name, LocalDate birthdate) {
this.name = name;
this.birthdate = birthdate;
}
Child() {
}
}
@Data
@EqualsAndHashCode(callSuper = true)
public class Adult extends Person {
private String job;
public Adult(String name, Date birthdate, String job) {
super(name, birthdate);
this.job = job;
}
Adult() {
}
}
@Data
@EqualsAndHashCode(callSuper = true)
public class Child extends Person {
private List<String> toys;
public Child(String name, Date birthdate, List<String> toys) {
super(name, birthdate);
this.toys = toys;
}
Child() {
}
}
关于java - swagger2 多态性 : add list of abstract classes to swagger documentation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43365041/
我完全卡在了我的一个显示错误的类中: ResettableHttpServletResponse is not abstract and does not override abstract meth
我遇到了这个错误,这让我很困惑。我不知道如何解决它。我知道那里还有一些其他错误,但我现在不太担心这些错误。 代码如下: import java.awt.*; public abstract class
我在调试此代码时遇到困难。我已经尝试了很多替代方法来消除该错误,但由于我是 Java 新手,所以我似乎无法指出问题所在。 public abstract class Animal { priv
我正在从 PHP 转向 C#。 在 PHP 中,使用抽象类创建“级联覆盖”模式简单直接,基本上“基类方法将处理它,除非继承类有具有相同签名的方法”。 然而,在 C# 中,我只花了大约 20 分钟尝试关
好吧,我正在尝试为 Bukkit/Spigot 编译一个 java 插件,但我收到以下错误: [ERROR] Failed to execute goal org.apache.maven.plugi
我有一个包含两个类(一个抽象类,一个非抽象类)的抽象类。我想从非抽象方法中调用抽象方法。 这就是我想要做的: class abstractClass { public: virtu
我最近遇到了以下方法。我尝试使用谷歌搜索并做了一个示例,通过如下定义方法来查看差异;两者似乎是一样的。但是,我需要知道它是否真的一样? public abstract void methodName(
在尝试创建可调用类时,我似乎遇到了上述错误。我已经搜索过原因但似乎找不到任何东西。 NetBeans 为我提供了一些使事情变得抽象的选项,但我对此很陌生,我宁愿找出发生某些事情的原因。谁能解释一下吗?
我无法编译下面编写的代码,Java 总是抛出错误。你能帮我一下吗(P.S:我是Java新手,仍处于学习阶段)。如果我的代码有写错的地方,请帮我改正,以便我能学好。 抛出错误 (4):1.加法不是抽象的
错误: java:com.company.Bicycle is not abstract and does not override abstract method applyBreakes(int)
拜托,每当我尝试构建我的 android 项目时,下面的方法都会出错。 方法: query.findInBackground(new FindCallback() { @Ove
关闭。这个问题是opinion-based 。目前不接受答案。 想要改进这个问题吗?更新问题,以便 editing this post 可以用事实和引文来回答它。 . 已关闭 3 年前。 Improv
我正在使用 pandas excelWriter 编写 Excel 文件。该程序运行没有任何问题,但我想删除此警告。 Abstract class 'ExcelWriter' with abstrac
我对 Java 非常陌生,而且我是一个痴迷于 GUI 的怪胎,因此被 Swing 所吸引。我对 Java 没有太多经验(真正的基础知识),但我已经开始制作游戏了。我想使用 Swing,并在网站的指导下
我从命令行创建了一个项目,但有八个错误。我之前在 Eclipse 中这样做过,遇到了这个错误,所以我想我会从外部尝试一下,以确保我没有做一些奇怪的事情。不管怎样,除了创建项目之外,绝对没有对项目做任何
我想根据卷号对数据对象列表进行排序。但以下代码无法与比较器一起使用。 错误: prog.java:15: error: GFG is not abstract and does not overrid
谁能解释为什么这在 JDK 1.6 中编译,但在 JDK 1.7 中却没有,我从中得到了错误消息: java: Example is not abstract and does not overrid
我有一个扩展 ListActivity 的基类,我想用它来实现许多用于多个不同 Activity 实现的通用代码。 我想将这个基类抽象化以强制在子类上实现方法。 如果我编写该方法的基本实现,并在子类中
我有一个包含大约 10 个 JTextField 的 JPanel,我希望在单击键盘上的向上或向下箭头后能够将焦点设置在特定文本字段上。因此,如果中间的文本字段获得焦点,并且单击向上箭头,则焦点将转到
我需要帮助解决问题。这可能很简单,但我不知道该怎么做! 我的错误: error: MainActivity is not abstract and does not override abstract
我是一名优秀的程序员,十分优秀!