Swagger with embedded Jetty

时间:2015-08-13 10:32:32

标签: java swagger embedded-jetty

有人可以告诉我这是否是使用嵌入式码头配置Swagger的正确方法。

public class TestMain {

public static void main(String[] args) throws Exception {
    Server gs = new Server();
    ServletContextHandler sch = gs.getServletContextHandler();

    sch.addFilter(new FilterHolder(new RequestTrackerFilter()), "/*",     EnumSet.of(DispatcherType.REQUEST));
    sch.addFilter(new FilterHolder(new ActionIdFilter()), "/*", EnumSet.of(DispatcherType.REQUEST));

    ResourceHandler resource_handler = new ResourceHandler();
    resource_handler.setDirectoriesListed(false);

    resource_handler.setResourceBase("public/2.0");

    ContextHandler context = new ContextHandler("/apitest");

    context.setHandler(resource_handler);

    ContextHandlerCollection contexts = new ContextHandlerCollection();


   setupSwaggerContextHandler();

  //     context.getServletContext().addListener(new SwaggerInitializer());

    contexts.setHandlers(new Handler[]{sch, context});

    gs.getServer().setHandler(contexts);

    gs.start();
}

private static ServletContextHandler setupSwaggerContextHandler() {
    // Configure Swagger-core
    final ServletHolder swaggerServletHolder = new ServletHolder(new JerseyJaxrsConfig());
    swaggerServletHolder.setName("JerseyJaxrsConfig");
    swaggerServletHolder.setInitParameter("api.version", "1.0.0");
    swaggerServletHolder.setInitParameter("swagger.api.basepath", "http://localhost:9000/apitest");
    swaggerServletHolder.setInitOrder(2);

    final ServletContextHandler swaggerContextHandler = new ServletContextHandler();
    swaggerContextHandler.setSessionHandler(new SessionHandler());
    // Bind Swagger-core to the url HOST/api-docs
    swaggerContextHandler.setContextPath("/api-docs");
    swaggerContextHandler.addServlet(swaggerServletHolder, "/*");

    return swaggerContextHandler;
  }
}

我无法使用

看到任何输出
  http://localhost:9000/apitest/api-docs

https://groups.google.com/forum/#!topic/swagger-swaggersocket/KHYESESD6c4

的讨论之后

2 个答案:

答案 0 :(得分:4)

我玩了很多让Swagger使用没有web.xml或Spring的嵌入式Jetty,最后得到了这个解决方案: https://github.com/SriramKeerthi/swagger-jersey2-jetty

如果我有足够的声誉,我会将其添加为评论。但这是我能提出的最简单的代码版本。你需要两个部分:

  1. Swagger Bean配置:

    BeanConfig beanConfig = new BeanConfig();
    beanConfig.setVersion("1.0.0");
    beanConfig.setResourcePackage(YourJerseyResourceClass.class.getPackage().getName());
    beanConfig.setScan(true);
    beanConfig.setBasePath("/");
    beanConfig.setDescription("Sample API to demonstrate Swagger with Jersey2" +
            " in an embedded Jetty instance, with no web.xml or Spring MVC.");
    beanConfig.setTitle("Sample API");
    
  2. 添加:

    ResourceConfig resourceConfig = new ResourceConfig();
    // Add your resources and Swagger resources (ApiListingResource package)
    resourceConfig.packages(YourJerseyResourceClass.class.getPackage().getName(), ApiListingResource.class.getPackage().getName());
    ServletContainer servletContainer = new ServletContainer(resourceConfig);
    ServletHolder servletHolder = new ServletHolder(servletContainer);
    ServletContextHandler servletContextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
    servletContextHandler.setContextPath("/");
    servletContextHandler.addServlet(servletHolder, "/*");
    
  3. 您现在应该可以在/swagger.json端点看到Swagger spec

    P.S。 irc.freenode.net上#swagger的团队非常出色,如果没有修复,你应该访问它们。

答案 1 :(得分:0)

在找到解决方案之前,我被困了几个小时。尽管我的配置与OP略有不同,但在此处添加它。就我而言,我只需要将几行配置插入我的嵌入式Jetty和RestService

// Jetty configuration

// ContextHandlerCollection contexts

ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/api");
context.addFilter(GzipFilter.class, "/*", EnumSet.allOf(DispatcherType.class));

ResourceConfig resourceConfig = new ResourceConfig(ImmutableSet.<Class<?>>builder()
                                                                .add(MyRestService.class)
                                                                .build());

resourceConfig.packages( ApiListingResource.class.getPackage().getName() ); // THIS WAS ONLY THING IN JETTY CONFIG THAT WAS NEEDED
context.addServlet(new ServletHolder(new ServletContainer(resourceConfig)), "/*");
contexts.addHandler(context);    

在其余服务上,我必须添加:

@Api(value = "MyRestService")
public class MyRestService
{
    @ApiOperation(notes = "Get object api", value = "Get objects")
    public Response getObjects() {....}

}

编辑 如果您希望配置诸如basepath之类的swagger属性,则可以使用BeanConfig进行配置,它是自包含的:

BeanConfig beanConfig = new BeanConfig();
        beanConfig.setVersion( "1.0.0" );
        beanConfig.setResourcePackage( SalusRestService.class.getPackage().getName() );
        beanConfig.setScan( true );
        beanConfig.setBasePath( "/api/v1" );
        beanConfig.setDescription( "Entity Browser API to demonstrate Swagger with Jersey2 in an " + "embedded Jetty instance, with no web.xml or Spring MVC." );
        beanConfig.setTitle( "Entity Browser" );