我通过Java注释生成的JSON swagger文档遇到了一些麻烦(REST使用Jersey公开,序列化由jackson处理).查看生成的swagger,它包含空值,这会导致swagger UI崩溃(生成的YAML没有此问题).
这里是JSON的摘录:
{
"swagger": "2.0",
"info": { "description": null, "version": "1.0.0", "title": "", "termsOfService": null, "contact": null, "license": null },
"host": "localhost:8080",
"basePath": "/api",
"tags": [ { "name": "dataset", "description": null, "externalDocs": null } ],
"schemes": [ "http" ],
"consumes": null,
"produces": null,
"paths": {
"/dataset": {
"get": {
"tags": ["dataset"],
"summary": "A dataset",
"description": "All the available datasets",
"operationId": "datasetGet",
"schemes": null,
"consumes": null,
"produces": ["application/json"],
"parameters": [],
"responses": {
"200": {
"description": "Available datasets",
"schema": {
"type": "object",
"format": null,
"example": null,
"xml": null,
"position": null,
"description": null,
"title": null,
"readOnly": null,
"additionalProperties": {
"type": "object",
"format": null,
"example": null,
"xml": null,
"position": null,
"description": null,
"title": null,
"readOnly": null,
"additionalProperties": {
"type": "object",
"format": null,
"example": null,
"xml": null,
"position": null,
"description": null,
"title": null,
"readOnly": null,
"properties": null
}
}
},
"examples": null,
"headers": null
}
},
"security": null,
"externalDocs": null,
"deprecated": null
},
"head": null,
"post": null,
"put": null,
"delete": null,
"options": null,
"patch": null,
"parameters": null
}
},
"parameters": null,
"responses": null,
"externalDocs": null,
"securityRequirement": null
}
此解决方案建议将对象映射器设置为省略空值以解决问题,但实际情况是我不知道在何处或如何轻松访问映射器,并且在REST服务类上使用NON_NULL注释是没有意义的.类似的问题已经被问为好,但我不使用Spring(所以我没有地方设置自定义映射器或设置为它).
我过去使用过Swagger,但是使用不同的配置设置(结合wink,现在我正在使用平针织)并且没有这个问题.
如果有一些明显的错误配置,这是我的web.xml:
MyProject
jersey
org.glassfish.jersey.servlet.ServletContainer
jersey.config.server.provider.classnames
io.swagger.jersey.listing.ApiListingResourceJSON,
org.something.DatasetApi
1
jersey
/service/*
DefaultJaxrsConfig
io.swagger.jaxrs.config.DefaultJaxrsConfig
api.version
1.0.0
swagger.api.basepath
http://localhost:8080/api
2
ApiOriginFilter
org.something.ApiOriginFilter
ApiOriginFilter
/*
这是我的pom.xml(我总是与swagger依赖关系混淆):
org.something
my_project
0.0.1-SNAPSHOT
4.0.0
war
My Project
http://maven.apache.org
MyProject
1.6
1.6
UTF-8
ch.qos.logback
logback-classic
1.0.13
junit
junit
4.8.1
test
org.glassfish.jersey.containers
jersey-container-servlet
2.6
org.glassfish.jersey.core
jersey-client
2.6
javax.servlet
javax.servlet-api
3.1.0
com.fasterxml.jackson.jaxrs
jackson-jaxrs-json-provider
2.4.2
io.swagger
swagger-jersey2-jaxrs
1.5.4
最后,为了完整性,这是我的Java类的摘录:
package org.something;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.Map;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Path("/dataset")
@Produces({ "application/json" })
@Api(value = "/dataset", description = "the dataset API")
public class DatasetApi {
private static final Logger log = LoggerFactory.getLogger(DatasetApi.class);
@GET
@ApiOperation(value = "All the available datasets.", notes = "Available datasets", response = Map.class, responseContainer = "Map")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Map of the available datasets", response = Map.class, responseContainer = "Map") })
public Map datasetGet() {
try {
log.info("Returning list of dataset");
return Storage.datasets().getDatasets();
} catch(Exception e) {
log.info("Exception!", e);
throw new WebJsonException(e, Response.Status.INTERNAL_SERVER_ERROR);
}
}
}
理想情况下,我想要一个以前调用过的类或者包含swagger逻辑的类(通过在web.xml中声明它)并做一个简单的null
解决所有麻烦的类,但我不知道这是否以及如何可能.
我已经花了一个星期的时间,我在发布问题后几个小时就找到了解决方案,对不起.
似乎在web.xml
缺少io.swagger.jaxrs.listing.SwaggerSerializers
ServletContainer中的类时,完整部分是:
jersey org.glassfish.jersey.servlet.ServletContainer jersey.config.server.provider.classnames io.swagger.jersey.listing.ApiListingResourceJSON, io.swagger.jaxrs.listing.SwaggerSerializers, org.something.DatasetApi 1
随着io.swagger.jaxrs.listing.SwaggerSerializers
一切,现在一切都按预期工作.