Swagger和Google Guice设置 [英] Swagger and Google Guice setup
问题描述
我正在努力向我的项目添加招摇.我们的设置与示例项目有所不同.我们使用guice和guice-servlet注入并启动我们的JerseyServletModule.
I'm trying to add swagger to my project. Our setup differs a bit from the sample projects. We use guice and guice-servlet to inject and start our JerseyServletModule.
今天我们的web.xml看起来像这样
Today our web.xml looks something like this
<web-app ....>
<listener>
<listener-class>com.mypackage.MyServletModule</listener-class>
</listener>
<filter>
<filter-name>guiceFilter</filter-name>
<filter-class>com.google.inject.servlet.GuiceFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>guiceFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
MyServletModule类看起来像
And the class MyServletModule looks like
public class MyServletModule extends GuiceServletContextListener {
...
@Override
protected Injector getInjector() {
JerseyServletModule api = new JerseyServletModule() {
@Override
protected void configureServlets() {
...
bind().to()
...
serve("/api/v1/*").with(GuiceContainer.class);
}
};
return Guice.createInjector(api);
}
}
我应该在哪里以及如何添加招摇呢?
Where and how should I add swagger?
推荐答案
您需要告诉Jersey在哪里可以找到这样的Swagger资源(其中 org.example
是包含服务的软件包):
You need to tell Jersey where to find the Swagger resources like this (where org.example
is your package containing services):
Map<String, String> params = Maps.newHashMap();
params.put(PackagesResourceConfig.PROPERTY_PACKAGES, "org.example;com.wordnik.swagger.jaxrs.listing");
serve("/api/v1/*").with(GuiceContainer.class, params);
还要确保Swagger在您的类路径中.如果您使用的是Maven,请添加:
Also be sure that Swagger is on your classpath. If you're using Maven add:
<dependency>
<groupId>com.wordnik</groupId>
<artifactId>swagger-jaxrs_2.9.1</artifactId>
<version>1.2.1</version>
<scope>compile</scope>
</dependency>
这篇关于Swagger和Google Guice设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!