Swagger与嵌入式Jetty

问题描述 投票:0回答:3

如果这是使用嵌入式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的讨论

java swagger embedded-jetty
3个回答
0
投票

如果您使用的是swagger-core 1.5.x,则需要在/swagger.json的上下文路径下查找。因此,为您的设置:

http://localhost:9000/apitest/api-docs/swagger.json


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, "/*");

您现在应该能够在spec端点看到Swagger /swagger.json

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


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());
// BELOW LINE WAS THE ONLY THING IN JETTY THAT WAS NEEDED               
resourceConfig.packages( ApiListingResource.class.getPackage().getName() );

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() {....}

}

编辑如果您希望配置像路径一样的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" );
© www.soinside.com 2019 - 2024. All rights reserved.