Embedded Jetty/Swagger 1.5.3 - Swagger UI not loading

125 views
Skip to first unread message

Nick Cuneo

unread,
Aug 31, 2015, 2:06:36 PM8/31/15
to Swagger, ncu...@tycoint.com
Hello,

I'm trying to add swagger to my little rest service but having some problems getting swagger UI to come up.  I've managed to get this working in the past with swagger 1.3 on other projects (we use swagger at work), but wanted to use the latest version for myself. 

Instead of just copying/pasting the code, I figured it would be easier to share it, maybe someone is noticing something I'm missing/configured incorrectly.

https://github.com/Logikz/echo-nest-ws/tree/feature/swagger

I'm able to run this and see swagger.json just fine, I just can't get the UI to come up no matter how I choose to configure my context paths and such.

I suppose if you don't want to skim the code, here's the juicy bits:

private static Handler getSwaggerHandler() throws URISyntaxException {
    // Create servlet for the UI
    URL swaggerUi = Resources.getResource( Main.class, "webapp" );
    Resource urlResource = URLResource.newResource( swaggerUi );

    ResourceHandler swaggerUIHandler = new ResourceHandler();
    swaggerUIHandler.setDirectoriesListed( true );
    swaggerUIHandler.setBaseResource( urlResource );
    swaggerUIHandler.setWelcomeFiles( new String[]{ "index.html" } );

    ContextHandler context = new ContextHandler("/swagger");
    context.setHandler( swaggerUIHandler );


    return context;
}

private static Handler getRestHandler() {
    ResourceConfig resourceConfig = new ResourceConfig();
    resourceConfig.packages( NestResources.class.getPackage().getName(), ApiListingResource.class.getPackage()
                                                                                                 .getName() );

    ServletContainer container = new ServletContainer( resourceConfig );
    ServletHolder holder = new ServletHolder( container );
    ServletContextHandler context = new ServletContextHandler( ServletContextHandler.SESSIONS );

    context.setContextPath( "/" );
    context.addServlet( holder, "/api/v1/*" );

    // Open CORS filter
    FilterHolder filter = new FilterHolder();
    filter.setInitParameter("allowedOrigins", "*");
    filter.setInitParameter("allowedMethods", "POST,GET,OPTIONS,PUT,DELETE,HEAD");
    filter.setInitParameter("allowedHeaders", "*");
    filter.setInitParameter("allowCredentials", "true");
    filter.setFilter( new CrossOriginFilter() );
    context.addFilter( filter, "/*", EnumSet.of( DispatcherType.ASYNC, DispatcherType.REQUEST, DispatcherType.ERROR, DispatcherType.INCLUDE ) );

    //auto load swagger ui
    ServletHolder pathHolder = new ServletHolder(new SwaggerDocsServlet("/api/v1"));
    context.addServlet( pathHolder, SwaggerDocsServlet.PATH_SERVLET_ENDPOINT );

    return context;
}

Thanks,
Nick

Ron Ratovsky

unread,
Aug 31, 2015, 4:00:56 PM8/31/15
to Swagger, ncu...@tycoint.com
Hi Nick,

If you managed to expose swagger-ui when using swagger-core 1.3, there should be no problem with swagger-core 1.5 as those are completely independent.

Which URL do you try opening when opening the UI?

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
-----------------------------------------
http://swagger.io
https://twitter.com/SwaggerApi
-----------------------------------------

Nick Cuneo

unread,
Aug 31, 2015, 6:39:46 PM8/31/15
to Swagger, ncu...@tycoint.com
I've tried
/api/v1
/swagger
/api/v1/swagger
/
/swagger/api-docs
/api/v1/swagger/api-docs
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggersocket+unsub...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Ron Ratovsky

unread,
Sep 3, 2015, 3:32:09 PM9/3/15
to Swagger, ncu...@tycoint.com
Based on you configuration I'd expect it to be available at /swagger. Do you get 404?

To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
-----------------------------------------
http://swagger.io
https://twitter.com/SwaggerApi
-----------------------------------------

--
You received this message because you are subscribed to the Google Groups "Swagger" group.
To unsubscribe from this group and stop receiving emails from it, send an email to swagger-swaggers...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Ron Ratovsky

unread,
Sep 3, 2015, 3:35:23 PM9/3/15
to Cuneo, Nicholas, Swagger
And are you sure the static files are packaged correctly in your war?

On Thu, Sep 3, 2015 at 10:33 PM, Cuneo, Nicholas <ncu...@tycoint.com> wrote:
Yes

Thanks,
Nick

_____________________________
From: Ron Ratovsky <r...@swagger.io>
Sent: Thursday, September 3, 2015 12:32 PM
Subject: Re: Embedded Jetty/Swagger 1.5.3 - Swagger UI not loading
To: Swagger <swagger-sw...@googlegroups.com>
Cc: Cuneo, Nicholas <ncu...@tycoint.com>



This e-mail contains privileged and confidential information intended for the use of the addressees named above. If you are not the intended recipient of this e-mail, you are hereby notified that you must not disseminate, copy or take any action in respect of any information contained in it. If you have received this e-mail in error, please notify the sender immediately by e-mail and immediately destroy this e-mail and its attachments.

Ron Ratovsky

unread,
Sep 3, 2015, 3:53:27 PM9/3/15
to Cuneo, Nicholas, Swagger
It shouldn't, no. I admit I never remember where the files need to be located, I just let maven take care of it.

On Thu, Sep 3, 2015 at 10:47 PM, Cuneo, Nicholas <ncu...@tycoint.com> wrote:
It's located at the root of the war. Do you think it should be local to the main class?

Thanks,
Nick

Reply all
Reply to author
Forward
0 new messages