Skip to content

Instantly share code, notes, and snippets.

@natros
Created May 16, 2015 15:26
Show Gist options
  • Save natros/370c0093fa60379c3901 to your computer and use it in GitHub Desktop.
Save natros/370c0093fa60379c3901 to your computer and use it in GitHub Desktop.
import com.google.inject.AbstractModule;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.config.ScannerFactory;
import com.wordnik.swagger.jaxrs.config.ReflectiveJaxrsScanner;
import com.wordnik.swagger.jaxrs.listing.ApiListingResource;
import com.wordnik.swagger.jaxrs.listing.SwaggerSerializers;
import com.wordnik.swagger.models.*;
import org.reflections.Reflections;
import java.util.Set;
public class SwaggerModule extends AbstractModule {
private final Reflections reflections;
public SwaggerModule(Reflections reflections) {
this.reflections = reflections;
}
@Override
protected void configure() {
bind(ApiListingResource.class);
bind(SwaggerSerializers.class);
Info info = new Info();
info.setTitle("Swagger example");
info.setVersion("1.0.0");
MyReflectiveJaxrsScanner scanner = new MyReflectiveJaxrsScanner();
scanner.setReflections(reflections);
scanner.setInfo(info);
ScannerFactory.setScanner(scanner);
}
private static class MyReflectiveJaxrsScanner extends ReflectiveJaxrsScanner {
private Info info;
@Override
public Set<Class<?>> classes() {
return reflections.getTypesAnnotatedWith(Api.class);
}
@Override
public Swagger configure(Swagger swagger) {
swagger.setInfo(info);
swagger.setBasePath("/api");
swagger.scheme(Scheme.HTTP);
return swagger;
}
public void setInfo(Info info) {
this.info = info;
}
}
}
@mkpchiran
Copy link

How to use this with guice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment