swagger-codegen client: How to include jackson annotations on models

梦想与她 提交于 2019-12-22 09:49:22

问题


I'm using swagger-codegen to generate a rest client, but I get a problem, the service I'm consuming returns a model with an inheritance, the API model looks like this:

public class Person
{
    private List<Book> books;
    ...
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "typeClass")
@JsonSubTypes({ @JsonSubTypes.Type(value = Magazine.class) })
public class Book 
{
    //some prop
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "typeClass")
public class Magazine extends Book
{
    //some prop
}

The API model is annotated with jackson annotations to deal with inheritance. The API works ok. When I generate the client, the client models don't have the jackson annotations, so when I use the generated client to consume the API, it always deserializate using the Book class. It doesn't "see" the Magazine class. I think it's because the generated model does'nt have the jackson annotations to deal with inheritance.

How can I config the swagger-codegen to add the jackson annotations to the model.

Thanks a lot...


回答1:


Add the following line of code in your plugin configuration in pom.xml

<'library>$libraryValue<'/library>

$libraryValue can be based on which library you are using in your dependencies. Either jersey one if you are using Jersey 1.x.x or jersey2 if you are using Jersey 2.x.x.

Thanks https://stackoverflow.com/users/3695939/moondaisy for the headstart that you have provided.



来源:https://stackoverflow.com/questions/41616922/swagger-codegen-client-how-to-include-jackson-annotations-on-models

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!