question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Embedded Jetty with Jersey and Swagger - no web.xml

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
rhyccecommented, Dec 22, 2016

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

0reactions
otonielortegacommented, Jul 15, 2018
Read more comments on GitHub >

github_iconTop Results From Across the Web

Swagger + jaxrs + embedded jetty + no web.xml
I have maven project with embedded jetty server. I have already created apis using JAX-RS, which are working properly. Now I want to...
Read more >
Embedded Jetty with Jersey and Swagger - no web.xml
I am using Jetty in a project to implement a web server. Currently, I programmatically add one servlet to support a RESTful API...
Read more >
Swagger, Jersey2 and embedded Jetty - without strings - Caffinc
There were several using web.xml. I don't want to configure an XML. I just want to write Jersey APIs and have them just...
Read more >
Jetty + Jersey 2 + Swagger programmatic configuration ...
I'm trying to configure Swagger in an embedded application that exposes a REST API with jersey 2 and jetty without using web.xml file...
Read more >
Using Swagger with embedded Jetty and without magic
Only reflection-heavy, automagic, confusing configuration is allowed! Take Jersey as an example, where the vast majority of tutorials showing ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found