Swagger-core: Embedded Jetty with Jersey and Swagger - no web.xml

Created on 26 Nov 2015  路  13Comments  路  Source: swagger-api/swagger-core

I am using Jetty in a project to implement a web server. Currently, I programmatically add one servlet to support a RESTful API using Jersey. This all works fine.
However I would also like to incorporate swagger for documentation of the API. For now I have a small example server which just produces text services. However swagger is not producing any documentation and I am not sure if I am using it correctly.

Bean Config Use:

private static class Bootstrap extends HttpServlet {
        @Override
        public void init(ServletConfig config) throws ServletException {
            super.init(config);
            BeanConfig beanConfig = new BeanConfig();
            beanConfig.setVersion("1.0.2");
            beanConfig.setSchemes(new String[]{"http"});
            beanConfig.setHost("localhost:2008");
            beanConfig.setBasePath("/api");
            beanConfig.setResourcePackage("io.swagger.resources");
            beanConfig.setScan(true);
        }
    }

Main method which launches the jetty server:

public static void main(String[] args) {  
        int port = 2008;  
        Server jettyServer = new Server(port);
        ServletHolder servletHolder =
            new ServletHolder(
                org.glassfish.jersey.servlet.ServletContainer.class);  
        servletHolder.setInitParameter(
            "com.sun.jersey.config.property.resourceConfigClass",
            "com.sun.jersey.api.core.PackagesResourceConfig");  


        servletHolder.setInitParameter(
            ServerProperties.PROVIDER_PACKAGES,
            "rest.service, io.swagger.jaxrs.listing"
        );
        ServletContextHandler context = 
            new ServletContextHandler(ServletContextHandler.SESSIONS);

        context.setContextPath("/");
        context.addServlet(servletHolder, "/*");


        ServletHolder swaggerServlet = new ServletHolder(new 
        Bootstrap());
        context.addServlet(swaggerServlet, "/*");

        jettyServer.setHandler(context);

      try {
            jettyServer.start();  
            jettyServer.join();  
        } catch (Exception ex) {
            System.exit(1);  
        }  
    }  

Finally my service is (very simple, just for testing for now)

package rest.service;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import io.swagger.annotations.*;


// Plain old Java Object

// The class registers its methods for the HTTP GET request using the @GET annotation. 
// Using the @Produces annotation, it defines that it can deliver several MIME types,
// text, XML and HTML. 

// The browser requests per default the HTML MIME type.

//Sets the path to base URL + /hello
@Api(value = "myService",
     description = "My Config Service API",
     produces = "text/html",
     consumes = "text/html")
@Path("api/hello")
public class PlainTextSvc {

  // This method is called if TEXT_PLAIN is request
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey from Plain Text Service";
  }

  // This method is called if XML is request
  @GET
  @Produces(MediaType.TEXT_XML)
  public String sayXMLHello() {
    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
  }

  // This method is called if HTML is request
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
  }

} 

When I try to access http://localhost:2008/api/hello or http://localhost:2008/api I get
HTTP ERROR: 405

Problem accessing /swagger.json. Reason:

HTTP method GET is not supported by this URL

This topic has probably been covered before so apologies in advance.

Most helpful comment

Did you resolve this? I seem to be having a similar issue.

All 13 comments

If you take out your Swagger integration do your API calls work? Right now, it seems like the problem you have is not directly related to the Swagger integration but rather the way you configure jersey+jetty together.

Yes the API calls work fine without swagger. I was wondering if I need to do more than

servletHolder.setInitParameter(
ServerProperties.PROVIDER_PACKAGES,
"rest.service, io.swagger.jaxrs.listing"
);

to tell the server about swagger.

There are several guides in the wiki as to how to integrate swagger-core. While the way to do it may be slightly different when using embedded jetty, the concept is still the same.

Do you have any resources that are hosted directly at the context root of your application (@Path("/"))?

No, everything is under /api/. Is this a problem?

No, that's not a problem at all. Since it sounds like a sample project, any chance you can share the code itself?

src.tar.gz
Yes it is just a sample project so no problem. The source files are attached.
example/client is not relevant for this issue.
dto contains my Data Transfer Object Definition
rest/service contains the service classes.
RestServer2.java is my server with no swagger integration.
server/swagger/RestServerSwagger is my attempt at a server integrating swagger.

Thanks for that. It looks like it's missing a pom file or any other build mechanism. That makes it difficult to run and test. Do you have the full structure of the project?

I am building and running using Eclipse. So all I could send are the .classpath and .project files ?

That may help. How do you manage your dependencies then? That could be the source of the problem.

The target project i want to use swagger for is actually an embedded system. So I compile on one machine and execute on another.
Primarily for that reason I am manually managing dependencies which i know is not ideal. But the list of dependencies must be finite and possible to add manually?
My .project and .classpath are attached.

CLASSPATH.txt
PROJECT.txt

There are definitely missing dependencies in that list, so you'd need to take care of that. Also, I'd suggest upgrading to the latest swagger-core, that is, 1.5.4.

Did you resolve this? I seem to be having a similar issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

davidmerrick picture davidmerrick  路  3Comments

bodycombathuang picture bodycombathuang  路  5Comments

Garamda picture Garamda  路  4Comments

nice-kai picture nice-kai  路  3Comments

yntelectual picture yntelectual  路  3Comments