为什么Swagger + RESTEasy不能使用@ApplicationPath("")

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

环境 = JBoss 7.2.0.Final + RESTEasy 2.3.5.Final + Swagger 1.3.10

尝试在没有 web.xml 的情况下设置 WAR 并使用 Swagger。如果 ApplicationPath 中有任何值,它就可以工作

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

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

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

适用于 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)

但是如果我改为:

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

服务和 API 文档可以工作,但 Swagger 似乎不可用。

我猜这是与 servlet 根上的侦听器发生冲突,但我有一个预先存在的约束,即服务在根 + 路径上运行,所以我需要一个空白的 ApplicationPath。

知道 Swagger 是否可以手动设置为运行不同的路径吗?

java jboss7.x resteasy swagger
2个回答
2
投票

问题在于您尝试从同一资源根服务应用程序和静态上下文,并且其背后存在一些技术问题。

我相信这个SO问题 - 根上下文上的JAX-RS应用程序 - 如何做到这一点? - 指的是同一件事,并且包含问题的广泛解决方案。


2
投票

虽然不能使用@ApplicationPath注释,但可以使用初始化参数设置应用程序路径:

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();
  } 
}

请参阅我的博客了解更多信息:
http://robferguson.org/2016/12/11/resteasy-embedded-jetty-fat-jars-swagger-and-swagger-ui/

以及我的 GitHub 存储库:
https://github.com/Robinyo/resteasy/tree/master/examples/fatjar-swagger

© www.soinside.com 2019 - 2024. All rights reserved.