I implemented a ReaderListener
in the same package as my resoure classes for Swagger. However, when the Swagger files are generated, this class is never used. I tried adding swagger.setBasePath("/empty");
to afterScan()
just to see if it was being called at all, to check if it changed the base path, but it didn't. Any idea on what I need to do to make sure this class is picked up in Swagger's scanning process? BTW, I am building the project with Maven and installing the generated RPMs if that makes a difference.
@io.swagger.annotations.SwaggerDefinition
public class SwaggerDefinition implements ReaderListener {
@Override
public void beforeScan(Reader reader, Scanner scanner) {
}
@Override
public void afterScan(Reader reader, Scanner scanner) {
final Model model = new ModelImpl()
.name("EntryContainer")
.type("Object")
.required("entry")
.property("entry", new ObjectProperty());
model.setReference("#/definitions/entry");
final Model model1 = new ModelImpl()
.name("Entry")
.type("Object")
.property("first_name", new StringProperty())
.property("last_name", new StringProperty())
.required("first_name")
.required("last_name");
Map<String, Model> defs = new HashMap<>();
definitions.put("EntryContainer", model);
definitions.put("Entry", model1);
swagger.setDefinitions(definitions);
}
}
Even though this is a old question I thought of posting what worked for me.
Here is my ReaderListener implemention.
import io.swagger.annotations.SwaggerDefinition;
import io.swagger.jaxrs.Reader;
import io.swagger.jaxrs.config.ReaderListener;
import io.swagger.models.Swagger;
@SwaggerDefinition
public class SwaggerBasePathModifier implements ReaderListener {
@Override
public void beforeScan(Reader reader, Swagger swagger) {
}
@Override
public void afterScan(Reader reader, Swagger swagger) {
// This is the purpose of my ReaderListener
// Yours can be different
swagger.setBasePath("/web-context/rest"); //<--set new base path
}
}
In my case SwaggerBasePathModifier class was not getting scanned by Swagger. To fix this I had to add this explicitly as one of the class to be scanned when my rest application initialized. See comments in the code.
classes.add(SwaggerBasePathModifier.class);
And here is the full class
@ApplicationPath("rest")
public class MyRESTApp extends Application {
public MyRESTApp() {
}
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<Class<?>>();
// api end points
classes.add(MyAPIResource.class);
// swagger related
classes.add(io.swagger.jaxrs.listing.ApiListingResource.class);
classes.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);
classes.add(SwaggerBasePathModifier.class);//<-- This does the trick
return classes;
}
}
This helped me to fix this.Hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With