本文介绍了swagger.json/swagger.yaml的错误404-非Maven的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在非Maven Java Jetty-Web应用程序中设置swagger.我已经从git存储库中提取了所有依赖项jar文件,并将它们包含在我的Web应用程序WEB-INF/lib目录中.但是我无法访问localhost:7443/api/swagger.json

I am trying set up swagger in a non-maven java jetty-web application.I have taken all the dependency jar files from the git repositories and included them in my web app WEB-INF/lib directory.Yet I am unable to access localhost:7443/api/swagger.json

Fyi,在我的Web应用程序中,我使用jersey 1.x和jax-rs.

Fyi, In my webapp I use jersey 1.x and jax-rs.

我已经按照文档中给出的方法进行了多种设置.

I have followed multiple ways to set up as given in the documentation.

方法1:Web-XML方式:

Approach-1:The web-xml way:

我已经在Web xml中添加了醒目的初始化和引用,如下所示:

I have added the swagger initialization and references in the web xml like this:

  <servlet>
    <servlet-name>jersey</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>com.sun.jersey.config.property.packages</param-name>
        <param-value>
            io.swagger.jaxrs.json,
            io.swagger.jaxrs.listing,
            com.myproject.rest
        </param-value>
    </init-param>
</servlet>

<servlet-mapping>
    <servlet-name>jersey</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>

    <servlet>
    <servlet-name>Jersey2Config</servlet-name>
    <servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
    <init-param>
        <param-name>api.version</param-name>
        <param-value>1.0.0</param-value>
    </init-param>
    <init-param>
        <param-name>swagger.api.basepath</param-name>
        <param-value>https://localhost:7443/api</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>

....此后定义了更多servlet和过滤器

....some more servlets and filters defined after this

构建和运行成功,并且我的Web应用程序像往常一样启动.我在运行时看到了以下日志行:

The build and run were successful and my web-app comes up like as usual. I see these log lines during the runtime:

15:53:36,837 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Could NOT find resource [logback.groovy]
15:53:36,837 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [logback-test.xml] at [jar:file:/Users/vinayabhishek/Documents/TCPWave/workspace/tims/WebContent/WEB-INF/lib/swagger-core-1.5.11-SNAPSHOT-tests.jar!/logback-test.xml]
15:53:36,848 |-INFO in ch.qos.logback.core.joran.spi.ConfigurationWatchList@32c26e28 - URL [jar:file:/Users/vinayabhishek/Documents/TCPWave/workspace/tims/WebContent/WEB-INF/lib/swagger-core-1.5.11-SNAPSHOT-tests.jar!/logback-test.xml] is not of type file
15:53:36,866 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
15:53:37,172 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
15:53:37,175 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
15:53:37,225 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - This appender no longer admits a layout as a sub-component, set an encoder instead.
15:53:37,225 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - To ensure compatibility, wrapping your layout in LayoutWrappingEncoder.
15:53:37,225 |-WARN in ch.qos.logback.core.ConsoleAppender[STDOUT] - See also http://logback.qos.ch/codes.html#layoutInsteadOfEncoder for details
15:53:37,225 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [io.swagger] to ERROR
15:53:37,225 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to ERROR
15:53:37,225 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
15:53:37,226 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
15:53:37,227 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@5e9771a5 - Registering current configuration as safe fallback point

但是 https://localhost:7443/api/swagger.json 给了我404响应.

However https://localhost:7443/api/swagger.json gives me 404 response.

方法2:自定义应用程序"类

我已经定义了一个自定义的SampleApplication.java类,看起来像这样

I have defined a custom SampleApplication.java class that looks like this

public class SampleApplication extends Application {

  public SampleApplication() {
    BeanConfig beanConfig = new BeanConfig();
    beanConfig.setVersion("1.0.2");
    beanConfig.setSchemes(new String[]{"http"});
    beanConfig.setHost("localhost:8002");
    beanConfig.setBasePath("/api");
    beanConfig.setResourcePackage("io.swagger.resources");
    beanConfig.setScan(true);
    System.out.println("Did we set it?");
}

  @Override
  public Set<Class<?>> getClasses() {
      Set<Class<?>> resources = new HashSet<Class<?>>();

      resources.add(PersonsService.class);
      //resources.add(SecondResource.class);
      //...

      resources.add(io.swagger.jaxrs.listing.ApiListingResource.class);
      resources.add(io.swagger.jaxrs.listing.SwaggerSerializers.class);

      return resources;
  }
}

这也不起作用.除了上面列出的 logback 内容外,我没有在堆栈跟踪中看到任何错误.

This also did not work. I do not see any errors in the stack trace except for the logback stuff listed above.

我还向我的WebService类之一添加了注释,如下所示:

I have also added annotations to one of my WebService class like this:

@Path("/people")
@Api( value = "/people", description = "Manage people" )

public class PeopleService {

  @Path("/list/{city_id}")
  @GET
  @Consumes({ MediaType.TEXT_PLAIN })
  @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })

  @ApiOperation(
      value = "List all people",
      notes = "List all people using paging",
      response = People.class,
      responseContainer = "List"
  )
  public ArrayList<People> list(....){


  }
}

我真的很感谢在此方面的任何帮助.预先谢谢你:)

I really appreciate any help on this. Thank you in advance :)

推荐答案

我遇到了同样的问题,并通过确保引用了正确的swagger依赖项来解决了该问题.最初,我包含 swagger-core ,但是对于我的Jersey 2.0项目,我需要使用 swagger-jersey2-jaxrs .我的建议是仔细检查您的图书馆.

I've just had the same problem and solved it by ensuring that I was referencing the correct swagger dependency.Originally I included swagger-core but for my Jersey 2.0 project I needed to use swagger-jersey2-jaxrs. My advice is to double check your libraries.

这篇关于swagger.json/swagger.yaml的错误404-非Maven的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 09:08