Swagger JaxRS可以使用鉴别符进行ApiModel继承吗? [英] Can Swagger JaxRS do ApiModel Inheritance with discriminator?

查看:920
本文介绍了Swagger JaxRS可以使用鉴别符进行ApiModel继承吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过使用Swagger JaxRs当前的主版本1.0和Swagger 2.0的devel_2.0分支.

I've tried with the Swagger JaxRs current master 1.0, and the devel_2.0 branch for Swagger 2.0.

@ApiModel(value = "Animal", 
  subTypes = {Dog.class, Lion.class}, 
  discriminator = "type")
public class Animal {

    @ApiModelProperty(value = "the discriminator field.")
    private String type;

这是子类之一,

@ApiModel(value = "Lion", parent = Animal.class)
public class Lion {

@ApiModelProperty(value = "the discriminator field.")
private String type;

我没有找到任何期望的例子,但这是我当前的Swagger 2.0项目swagger.json文件中的输出.

I haven't found any many examples of what to expect, but here is the output in my current Swagger 2.0 projects swagger.json file.

   "definitions":{
      "Animal":{
         "properties":{
            "type":{
               "type":"string",
               "description":"the discriminator field."
            }
         },
         "discriminator":"type"
      },

在定义中没有Dog或Lion对象的迹象.请求对象中没有任何内容.我不确定它是否会起作用,但是如果您知道它应该如何工作,请告诉我.

No sign of the Dog or Lion object under definitions. Nothing in the request object. I'm not sure what this would look like if it worked, but let me know if you know how it should work.

如果要查看完整的上下文,所有代码都在这里.

All the code is here if you want to see the full context.

https://github.com/javatestcase/RestEasy/tree/RestEasyVersion2

推荐答案

您的示例对我有很大帮助,所以我认为我应该为您提供帮助,因为我现在就可以使用它!

Your examples helped me alot, so I thought I should help you in return because I got it working now!

您需要告诉序列化/反序列化如何绑定实现:

You need to tell the serialisation/deserialisation how to bind the implementation:

@JsonTypeInfo(
    use = JsonTypeInfo.Id.NAME, // Were binding by providing a name
    include = JsonTypeInfo.As.PROPERTY, // The name is provided in a property
    property = "type", // Property name is type
    visible = true // Retain the value of type after deserialisation
)
@JsonSubTypes({//Below, we define the names and the binding classes.
    @JsonSubTypes.Type(value = Lion.class, name = "Lion"),
    @JsonSubTypes.Type(value = Dog.class, name = "Dog")
})
@ApiModel(value = "Animal", subTypes = {Dog.class, Lion.class}, discriminator = "type")
public class Animal {

这篇关于Swagger JaxRS可以使用鉴别符进行ApiModel继承吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆