【问题标题】:Can Swagger JaxRS do ApiModel Inheritance with discriminator?Swagger JaxRS 可以用鉴别器做 ApiModel 继承吗?
【发布时间】:2015-02-21 08:11:59
【问题描述】:

我已尝试使用 Swagger JaxRs 当前的 master 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

【问题讨论】:

  • 如何运行示例?我正在尝试直接在 Jetty 上运行它,显然它不起作用,因为 pom 中没有 RESTEasy 依赖项(查看 Swagger 2 分支)。
  • @webron - 无法快速让 Jetty 或 Tomcat 运行,但包含 wildfly-maven-plugin。应该可以和 jetty 插件一样运行 -> localhost:8080/RestEasy-1/xyz/swagger.json.
  • 谢谢,我会检查一下,但可能只有明天。来晚了。

标签: jax-rs resteasy swagger


【解决方案1】:

你的例子对我帮助很大,所以我想我应该帮助你,因为我现在已经开始工作了!

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-26
    • 2014-04-20
    • 1970-01-01
    • 2016-08-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多