我正在尝试在非maven java jetty-web应用程序中设置swagger。 我从git存储库中获取了所有依赖jar文件,并将它们包含在我的Web应用程序WEB-INF / lib目录中。 但我无法访问localhost:7443 / api / swagger.json
Fyi,在我的webapp中,我使用了jersey 1.x和jax-rs。
我已按照文档中的说明设置了多种方式进行设置。
方法-1: web-xml方式:
我在web xml中添加了swagger初始化和引用,如下所示:
<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和过滤器
构建和运行成功,我的网络应用程序像往常一样出现。我在运行时看到了这些日志行:
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响应。
方法2:自定义应用程序类
我已经定义了一个类似于
的自定义SampleApplication.java类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 之外,我没有在堆栈跟踪中看到任何错误。
我还在我的一个WebService类中添加了注释:
@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(....){
}
}
我真的很感激这方面的任何帮助。提前谢谢你:)
答案 0 :(得分:0)
我刚刚遇到了同样的问题并通过确保我引用了正确的swagger依赖来解决了这个问题。 最初我包括 swagger-core ,但对于我的Jersey 2.0项目,我需要使用swagger-jersey2-jaxrs。我的建议是仔细检查你的图书馆。