我正在尝试使用 swagger-codegen 生成的代码来使用我的 REST 服务,该服务在我的 swagger.json 中定义(由 springfox 2.9.0 在服务器端提供)
我正在生成 api、模型和支持文件。
但是每次我尝试使用 GET api 时,都会发生错误:
avax.ws.rs.client.ResponseProcessingException: Problem with reading the data, class xxx.xxx.generatedmodels.MyEntity, ContentType: application/json;charset=UTF-8.
....
Caused by: org.codehaus.jackson.map.exc.UnrecognizedPropertyException: Unrecognized field "@id" (Class xxx.xxx.generatedmodels.MyEntity), not marked as ignorable
我的猜测是生成的 Model 类需要 class-annotation @JsonIdentityInfo(generator = JSOGGenerator.class)
,就像它们在服务器端模型上一样。(也可能@JsonIgnoreProperties(ignoreUnknown = true)
?)
这个假设正确吗?
如果是,那么有没有办法配置 swagger-codegen 以在每个模型上添加此注释?
如果没有,我该怎么做才能让 ApiClient 反序列化接收到的实体?
我的 swagger-codegen 配置如下所示:
<plugin>
<groupId>io.swagger</groupId>
<artifactId>swagger-codegen-maven-plugin</artifactId>
<version>2.3.1</version>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
<configuration>
<inputSpec>https://example.com/v2/api-docs</inputSpec>
<language>java</language>
<generateApis>true</generateApis>
<generateModels>true</generateModels>
<generateSupportingFiles>true</generateSupportingFiles>
<generateModelDocumentation>false</generateModelDocumentation>
<generateModelTests>false</generateModelTests>
<modelPackage>xxx.xxx.generatedmodels</modelPackage>
<apiPackage>xxx.xxx.generatedapi</apiPackage>
<invokerPackage>xxx.xxx.generatedinvoker</invokerPackage>
<configOptions>
<dateLibrary>java8</dateLibrary>
<sourceFolder>src/main/java</sourceFolder>
<java8>true</java8>
</configOptions>
<library>jersey2</library>
<output>../generated-client</output>
<groupId>xxx.xxx</groupId>
<artifactId>generatedapiclient</artifactId>
<artifactVersion>1.0.0</artifactVersion>
</configuration>
</execution>
</executions>
</plugin>
响应看起来像这样(严重缩短。有很多对象属性也有“@id”。这个例子与 swagger.json 进一步匹配):
{"@id":"1","id":180,"name":"Test entity"}
json 是有效的,我已经检查过了
编辑:这是我大大减少和去个性化的 swagger.json:
{
"swagger": "2.0",
"info": {
"description": "Api Documentation",
"version": "1.0",
"title": "Api Documentation",
"termsOfService": "urn:tos",
"contact": {
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0"
}
},
"host": "localhost:9080",
"basePath": "/",
"tags": [
{
"name": "foobar-controller",
"description": "REST operations available for FooBar"
}
],
"paths": {
"/api/fooBars": {
"get": {
"tags": [
"request-car-controller"
],
"summary": "Find a FooBar by id",
"operationId": "getBeanUsingGET",
"produces": [
"application/json"
],
"parameters": [
{
"name": "Authorization",
"in": "header",
"description": "Authorization",
"required": true,
"type": "string"
},
{
"name": "id",
"in": "query",
"description": "id",
"required": true,
"type": "integer",
"format": "int64"
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/FooBar"
}
},
"401": {
"description": "Unauthorized"
},
"403": {
"description": "Forbidden"
},
"404": {
"description": "Not Found"
}
},
"security": [
{
"token": [
]
}
]
}
}
},
"securityDefinitions": {
"Authorization": {
"type": "apiKey",
"name": "Authorization",
"in": "header"
}
},
"definitions": {
"FooBar": {
"type": "object",
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"name": {
"type": "string"
}
},
"title": "FooBar"
}
}
服务器端的 FooBar 类如下所示:
@Entity
@Table(name = "foobar")
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonIdentityInfo(generator = JSOGGenerator.class) // this is what generates the `@id` in the json. I need this.
public class FooBar implements Serializable {
private Long id;
private String name;
public FooBar(){
}
public FooBar(String name){
this.name = name;
}
// getters, setters
}