问题
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;
And here is one of the sub classes,
@ApiModel(value = "Lion", parent = Animal.class)
public class Lion {
@ApiModelProperty(value = "the discriminator field.")
private String type;
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"
},
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
回答1:
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 {
来源:https://stackoverflow.com/questions/27616061/can-swagger-jaxrs-do-apimodel-inheritance-with-discriminator