4

Environment = JBoss 7.2.0.Final + RESTEasy 2.3.5.Final + Swagger 1.3.10

Trying to set up a WAR with no web.xml and use Swagger. It works if there is any value in the ApplicationPath

@javax.ws.rs.ApplicationPath("test")

@WebServlet(name = "RestEasy-1", loadOnStartup = 1)

@Path("/message")
@Api(value="/message",description="hello api")

Works for URL

http://localhost:8080/RestEasy-1/test/message/xyz (THE SERVICE)
http://localhost:8080/RestEasy-1/test/api-docs (SHOWS SWAGGER JSON)
http://localhost:8080/RestEasy-1/ (RUNS SWAGGER UI)

However if I change to:

@javax.ws.rs.ApplicationPath("") (also tried /* or * or /)

The service and api-docs work, but Swagger doesn't seem to be available.

I guess it is a collision with listeners on the root of the servlet, but I have a pre-existing constraint that the services run at the root + path, so I need a blank ApplicationPath.

Any idea if Swagger can be set to run of a different path manually?

javatestcase
  • 682
  • 1
  • 10
  • 25
  • Can you clarify a few things? Swagger is a spec, not a library. When you say Swagger 1.3.10 I understand you mean swagger-core 1.3.10. I'm not sure what you mean by `http://localhost:8080/RestEasy-1/ (RUNS SWAGGER)` - what's SWAGGER here? – Ron Dec 14 '14 at 07:49
  • @webron swagger-ui, specifically the index.html of swagger-ui in the WEB-INF folder. – javatestcase Dec 14 '14 at 17:38
  • Have you find any solution for this? – Renan Geraldo Apr 16 '21 at 21:37

2 Answers2

2

The problem is that you're trying to serve both the application and static context from the same resource root, and there are some technical issues behind it.

I believe this SO question - JAX-RS Application on the root context - how can it be done? - refers to the same thing and contains an extensive solution to the problem.

Community
  • 1
  • 1
Ron
  • 14,160
  • 3
  • 52
  • 39
2

While you can't use the @ApplicationPath annotation, you can set the application path using an initialisation parameter:

package org.robferguson.resteasy.examples.fatjar;

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
import org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher;

public class Main {

  static final String APPLICATION_PATH = "/api";
  static final String API_PATH_SPEC = "/api/*";
  static final String SWAGGER_UI_PATH_SPEC = "/*";

  public Main() {}

  public static void main( String[] args ) throws Exception
  {
    try
    {
      new Main().run();
    }
    catch (Throwable t)
    {
      t.printStackTrace();
    }
  }

  public void run() throws Exception
  {
    final int port = 8080;
    final Server server = new Server(port);

    // setup Application context
    ServletContextHandler context = new ServletContextHandler();

    // setup JAX-RS (RESTEasy) resources
    ServletHolder apiServlet = new ServletHolder(
        new HttpServletDispatcher());
    apiServlet.setInitOrder(1);
    apiServlet.setInitParameter("resteasy.servlet.mapping.prefix",
        APPLICATION_PATH);
    apiServlet.setInitParameter("javax.ws.rs.Application",
        "org.robferguson.resteasy.examples.fatjar.FatJarApplication");

    // setup static (Swagger UI) resources
    String resourceBasePath = Main.class.getResource(
      "/swagger-ui").toExternalForm();
    context.setResourceBase(resourceBasePath);
    context.setWelcomeFiles(new String[] { "index.html" });

    ServletHolder swaggerUiServlet = new ServletHolder(
      new DefaultServlet());
    swaggerUiServlet.setInitOrder(2);

    context.addServlet(apiServlet, API_PATH_SPEC);
    context.addServlet(swaggerUiServlet, SWAGGER_UI_PATH_SPEC);

    server.setHandler(context);
    server.start();
    server.join();
  } 
}

See my blog for more information:
http://robferguson.org/2016/12/11/resteasy-embedded-jetty-fat-jars-swagger-and-swagger-ui/

As well as my GitHub repo:
https://github.com/Robinyo/resteasy/tree/master/examples/fatjar-swagger

Ian Campbell
  • 23,484
  • 14
  • 36
  • 57
Robinyo
  • 586
  • 4
  • 11