如何使用Guice&x2B将Swagger集成到Java项目中;泽西岛+;servlet并自动生成RESTAPI文档
我有一个应用程序,它使用Guice和Jersey提供REST服务。如何在Guice中配置Swagger?我已具有以下依赖项:如何使用Guice&x2B将Swagger集成到Java项目中;泽西岛+;servlet并自动生成RESTAPI文档,java,rest,jersey,swagger,guice,Java,Rest,Jersey,Swagger,Guice,我有一个应用程序,它使用Guice和Jersey提供REST服务。如何在Guice中配置Swagger?我已具有以下依赖项: compile group: 'io.swagger', name: 'swagger-jersey2-jaxrs', version: '1.5.0' 如何自动生成API文档?如果有人遇到过类似的问题,请解释一下这种整合。非常感谢 外表像: public class ServletContextListener extends GuiceServletContextL
compile group: 'io.swagger', name: 'swagger-jersey2-jaxrs', version: '1.5.0'
如何自动生成API文档?如果有人遇到过类似的问题,请解释一下这种整合。非常感谢
外表像:
public class ServletContextListener extends GuiceServletContextListener {
@Override
protected Injector getInjector() {
if(lifecycleInjector != null) return lifecycleInjector;
this.lifecycleInjector = InjectorBuilder
.fromModules(defineModules())
.createInjector(Stage.DEVELOPMENT);
return this.lifecycleInjector;
}
private List<Module> defineModules() {
ArrayList<Module> modules = new ArrayList<>();
modules.add(new ServletModule());
return modules;
}
}
公共类ServletContextListener扩展了GuiceServletContextListener{
@凌驾
受保护的喷油器getInjector(){
如果(lifecycleInjector!=null)返回lifecycleInjector;
this.lifecycleInjector=InjectorBuilder
.fromModules(defineModules())
.1.1(阶段发展);
返回此.lifecycleInjector;
}
私有列表定义模块(){
ArrayList模块=新的ArrayList();
modules.add(新的ServletModule());
返回模块;
}
}
Jersey Servlet模块类似于:
public class ServletModule extends JerseyServletModule {
@Override
protected void configureServlets() {
bind(GuiceContainer.class).to(GovernatorServletContainer.class).asEagerSingleton();
serve("/api/*").with(GuiceContainer.class);
}
@Provides
ResourceConfig getResourceConfig() {
final List<String> resources = new ArrayList<>();
resources.add("com.fasterxml.jackson.jaxrs.json");
final String[] arrayPackages = new String[resources.size()];
resources.toArray(arrayPackages);
return new RestApp(ImmutableMap.<String, Object>builder()
.put(PROPERTY_PACKAGES, arrayPackages)
.put(FEATURE_DISABLE_WADL, "true")
.build());
}
@SwaggerDefinition(tags = {@Tag(name = "app API", description = "Administrative API for app")})
@ApplicationPath(RestApp.BASE_REST_APP_URI)
public class RestApp extends PackagesResourceConfig {
public static final String BASE_REST_APP_URI = "/api";
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> classes = new HashSet<>();
classes.add(AppRestResource.class);
return classes;
}
}
公共类ServletModule扩展了JerseyServletModule{
@凌驾
受保护的void configureServlets(){
绑定(GuiceContainer.class).to(GovernatorServletContainer.class).asagerSingleton();
使用(GuiceContainer.class)提供(“/api/*”);
}
@提供
ResourceConfig getResourceConfig(){
最终列表资源=新的ArrayList();
resources.add(“com.fasterxml.jackson.jaxrs.json”);
最终字符串[]arrayPackages=新字符串[resources.size()];
资源。到阵列(阵列包);
返回新的RestApp(ImmutableMap.builder()
.put(属性包、阵列包)
.put(功能\u禁用\u WADL,“真”)
.build());
}
@招摇过市定义(标记={@Tag(name=“app API”,description=“app的管理API”)})
@应用程序路径(RestApp.BASE\u REST\u APP\u URI)
公共类RestApp扩展了PackageResourceConfig{
公共静态最终字符串BASE\u REST\u APP\u URI=“/api”;
@凌驾
public Set>classes=new HashSet();
添加(appresource.class);
返回类;
}
}
我如何才能添加这种炫耀?我无法帮助您使用Guice,但我可以为您的其余问题提供一些提示 为了配置Swagger,您需要像下面的示例一样配置Jersey servlet。我正在使用一个web.xml,也许有人可以帮你为这个配置一个Guice
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>sample.app.ws.MyJavaWSApplication</param-value>
</init-param>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>io.swagger.jaxrs.listing,sample.app.ws</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/v1/*</url-pattern>
</servlet-mapping>
为了访问文档,您可以使用Swagger UI生成一个漂亮的页面,如。我曾尝试使用运行时的org.webjars:swagger ui:2.1.4执行此操作,但最终我下载了文件夹并将其复制到我的webapp目录。首先感谢,我已解决了此问题。示例是请创建问题的详细信息,以便其他人能够最好地帮助您。您好,我已将整个swaggerui文件夹复制到我的项目中。尝试这样访问它:但不幸的是,这不起作用。你知道如何访问大摇大摆的用户界面吗?不过,访问功能运行良好。
package sample.app.ws;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import sample.app.model.Book;
@Path("/books")
@Produces(MediaType.APPLICATION_JSON)
@Api(value = "books")
public class BookService {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Creates a book", notes = "", response = Response.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "Successful creation"),
@ApiResponse(code = 500, message = "Resource could not be created")
})
public Response add(Book book) {
// Implementation code
}
}