问题描述
我只是大摇大摆地产生了一个有效的swagger.json.我使用Application-config方法配置了swagger.但是,一旦我重写getClasses-Method以添加大量的资源,我的带有JAX-RS Path注释的类就会停止工作.方法看起来像这样
I just got swagger to produces a valid swagger.json.I configured swagger by using the Application-config method.However, as soon as I override the getClasses-Method to add the swagger resouces, my JAX-RS Path-annotated classes stop working.The method looks like this
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new HashSet<>();
resources.add(io.swagger.jaxrs.listing.ApiListingResource.class);
resources.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);
return resources;
}
并调用super.getClasses()返回空集.我的项目中有太多资源,我不想手动添加.
and invoking super.getClasses() returns am empty set.I got too many resources in my project, which I would not like to add manually.
有什么招摇动的方式不会干扰我以前的配置吗?
Is there any way swagger does not mess with my previous configuration?
谢谢!
推荐答案
您可以使用 javax.ws.rs.core.Feature
.只需通过回调的FeatureContext
注册这些类.用@Provider
注释该功能将通过扫描将其注册.
You can use a javax.ws.rs.core.Feature
. Just register the classes through the callback's FeatureContext
. Annotating the feature with @Provider
will have it registered through the scanning.
@Provider
public class SwaggerFeature implements Feature {
@Override
public boolean configure(FeatureContext context) {
context.register(ApiListingResource.class);
context.register(SwaggerSerializers.class);
return true;
}
}
但是请注意,如果应用程序已经通过类路径扫描注册了资源和提供程序,我想它也应该使用Swagger类,因为它们使用@Path
和@Provider
.这些是类路径扫描要查找的注释.
But note that if the application is already registering the resources and providers by class-path scanning, I imagine it should also pick up the Swagger classes, as they are annotated with @Path
and @Provider
. Those are the annotations the class-path scan looks for.
我自己还没有尝试过(我停止使用类路径扫描),但是您是否尝试过根本不注册它们?从理论上讲,类路径扫描应该将其拾取.
I haven't tried it myself (I stopped using class-path scanning), but have you tried just not registering them at all? In theory the class-path scan should pick it up.
这篇关于Swagger会覆盖路径注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!