我正在试图以昂首阔步来记录休息服务。在构建和部署之后,我得到一个缺少标签的json(在我的情况下"tags" : ["databases"]
缺失)并完全忽略webservice接口方法的参数的注释(只有参数,其他文档正确提供) 。而不是列出它包含的参数:
"parameters" : [{
"in" : "body",
"name" : "body",
"required" : false,
"schema" : {
"type" : "string"
}
}
]
很可能它与配置或加载Web应用程序的过程有关,因为在使用tomcat管理器webapp重新加载web应用程序后,它会发送正确的json。
编辑:简单地停止并重新启动应用程序具有相同的效果。另外,重载或重启后的json不是确定性的(随机的)。这意味着问题应该在类加载器上。
我使用的是apache tomcat 8.0.33版,重新版本为3.0.14.Final
,而且招摇是:
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-jaxrs</artifactId>
<version>1.5.8</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.8</version>
</dependency>
应用程序类如下所示:
@ApplicationPath("/")
public class ServiceApplication extends Application {
final Set<Object> singletons = new HashSet<Object>();
public ServiceApplication() throws IOException {
final InputStream inStream = getClass().getClassLoader()
.getResourceAsStream("swagger.properties");
final Properties props = new Properties();
props.load(inStream);
final BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.0");
beanConfig.setHost(props.getProperty("host"));
beanConfig.setBasePath("/rest-service");
beanConfig.setResourcePackage("com.example.package");
beanConfig.setScan(true);
final CorsFilter filter = new CorsFilter();
filter.getAllowedOrigins().add("*");
singletons.add(filter);
}
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> resources = new HashSet<Class<?>>();
// add WS classes
resources.add(DatabasesImpl.class);
// add provider classes
resources.add(ExcMapper1.class);
resources.add(ExcMapper2.class);
resources.add(ResponseInterceptor.class);
// add swagger classes
resources.add(io.swagger.jaxrs.listing.ApiListingResource.class);
resources.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);
return resources;
}
@Override
public Set<Object> getSingletons() {
return singletons;
}
}
网络服务上的注释:
@SwaggerDefinition(info = @io.swagger.annotations.Info(description = "<DESCRIPTION>",//
version = "0.0.2-SNAPSHOT", title = "Search", termsOfService = "<TOS>",//
contact = @io.swagger.annotations.Contact(name = "<CONTACT_NAME>", email = "<CONTACT_EMAIL>", url = "<CONTACT_URL>"),//
license = @io.swagger.annotations.License(name = "<LICENCE_NAME>", url = "<LICENCE_URL>")), consumes = {}, produces = { MediaType.APPLICATION_XML },//
schemes = { SwaggerDefinition.Scheme.HTTP, SwaggerDefinition.Scheme.HTTPS },//
externalDocs = @io.swagger.annotations.ExternalDocs(value = "<EXTERNAL_DOCS>", url = "<EXTERNAL_DOCS_URL>"))
@Api(value = "/databases")
@Path("/databases/")
public interface Databases {
@ApiOperation(value = "Searches the database")
@ApiResponses(value = { @ApiResponse(code = 200, message = "Success"),
@ApiResponse(code = 404, message = "Database not found") })
@GET
@Path("{database}/search")
@Produces(MediaType.APPLICATION_XML)
public Response searchExport(
@ApiParam(value = "the database name", required = true, defaultValue = "default") @PathParam("database") @Nonnull final String database,
@ApiParam(value = "query string defaults to match all", defaultValue = "title:foo") @QueryParam("q") @DefaultValue("*:*") final String query,
@ApiParam(value = "starting element index defaults to 0") @QueryParam("start") @DefaultValue("0") final int start,
@ApiParam(value = "number of elements to return defaults to 10") @QueryParam("rows") @DefaultValue("10") final int rows,
@ApiParam(value = "sorting field") @QueryParam("sort") @DefaultValue("title") final String sort,
@ApiParam(value = "sorting order defaults to ascending") @QueryParam("sortOrder") @DefaultValue("ascending") final SortDirection sortOrder)
throws ParseException;