Swagger JaxRS能够使用鉴别器实现ApiModel继承吗?

5
我尝试过使用Swagger JaxRs当前的主版本1.0,以及Swagger 2.0的devel_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文件的输出结果。

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

没有在定义中找到Dog或Lion对象的迹象。请求对象中也没有任何东西。如果这个功能能够正常工作,我不确定它的样子会是什么样子,但如果您知道它应该如何工作,请告诉我。
如果您想查看完整的上下文,所有代码都在这里。 https://github.com/javatestcase/RestEasy/tree/RestEasyVersion2

1
你怎么运行这个示例?我试图直接在Jetty上运行它,但显然不起作用,因为pom中没有RESTEasy依赖项(查看Swagger 2分支)。 - Ron
@webron - 没有能够快速启动Jetty或Tomcat,但包含了wildfly-maven-plugin。应该可以像jetty插件一样运行 -> http://localhost:8080/RestEasy-1/xyz/swagger.json。 - javatestcase
谢谢,我会查看的,但可能要明天才行。这里已经很晚了。 - Ron
1个回答

5

你的示例对我很有帮助,所以我想回报一下,因为现在它已经可以工作了!

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

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接