swagger-codegen客户端:如何在模型上包括杰克逊注释 [英] swagger-codegen client: How to include jackson annotations on models
问题描述
我正在使用swagger-codegen生成一个rest客户端,但是遇到一个问题,我使用的服务返回了一个带有继承的模型,API模型如下所示:
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
}
API模型使用jackson批注进行批注以处理继承.该API正常运行. 当我生成客户端时,客户端模型没有杰克逊注释,因此当我使用生成的客户端使用API时,它总是使用Book类反序列化.它没有看到"杂志类.我认为这是因为生成的模型没有用于处理继承的杰克逊注释.
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.
如何配置swagger-codegen以将杰克逊注释添加到模型中.
How can I config the swagger-codegen to add the jackson annotations to the model.
非常感谢...
推荐答案
而不是使用
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate
-i http://petstore.swagger.io/v2/swagger.json
-l java
-o samples/client/petstore/java
您可以使用
java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate
-i http://petstore.swagger.io/v2/swagger.json
-l spring
-o samples/client/petstore/java
通过语言更改,他们还将gson换成Jackson.
Through the language change they also swap out gson to Jackson.
在您的情况下,您将拥有类似的东西
In your case you wwould have something like
import java.util.Objects;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.model.Category;
import io.swagger.model.Tag;
import java.util.ArrayList;
import java.util.List;
import org.springframework.validation.annotation.Validated;
import javax.validation.Valid;
import javax.validation.constraints.*;
/**
* Pet
*/
@Validated
@javax.annotation.Generated(value = "io.swagger.codegen.languages.SpringCodegen", date = "2020-06-04T13:33:18.844+02:00")
public class Pet {
@JsonProperty("id")
private Long id = null;
@JsonProperty("category")
private Category category = null;
@JsonProperty("name")
private String name = null;
@JsonProperty("photoUrls")
@Valid
private List<String> photoUrls = new ArrayList<String>();
@JsonProperty("tags")
@Valid
private List<Tag> tags = null;
...
来源: https://github.com/swagger-api/swagger -codegen/issues/5785
使用Generator.Swagger构建客户端时( https://generator.swagger.io/在线招摇的Codegen api:/gen/clients/{language}),然后您就可以浏览/gen/clients/{language}或
When you build the client with Generator.Swagger (https://generator.swagger.io/ the online swagger Codegen api:/gen/clients/{language}) then you can look through the settings in /gen/clients/{language} or https://openapi-generator.tech/docs/generators/java/ and build your request up to that.
这篇关于swagger-codegen客户端:如何在模型上包括杰克逊注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!