我试图将Swagger包含在我的项目中。 我使用WildFly(10)服务器和RestEasy 3.0.11,我的资源是由EJB驱动的,整个项目都是用Ant构建的。
Swagger依赖项由WildFly模块提供。
问题是当我试图获得swagger.json时(通过 http://localhost:8080/app/api/swagger.json ,但我也尝试了其他变体)I'在浏览器中有一个404响应/空白页面并跟随控制台输出:
09:40:49,893 WARN [org.jboss.resteasy.core.ExceptionHandler] (Log4jLogger.java:135) failed to execute: javax.ws.rs.NotFoundException: Could not find resource for full path: http://localhost:8080/app/api/swagger.json
at org.jboss.resteasy.core.registry.SegmentNode.match(SegmentNode.java:112)
at org.jboss.resteasy.core.registry.RootNode.match(RootNode.java:43)
at org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.java:48)
at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:444)
at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:234)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:171)
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:86)
at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58)
at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:72)
at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:282)
at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:261)
at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:80)
at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:172)
at io.undertow.server.Connectors.executeRootHandler(Connectors.java:199)
at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:774)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:748)
web.xml 配置如下:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<display-name>app</display-name>
<description>app</description>
<context-param>
<param-name>resteasy.scan</param-name>
<param-value>true</param-value>
</context-param>
<servlet-mapping>
<servlet-name>javax.ws.rs.core.Application</servlet-name>
<url-pattern>*.json</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>SwaggerServlet</servlet-name>
<servlet-class>my.example.app.examples.web.SwaggerServlet</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
</web-app>
这是上面提到的SwaggerServlet:
import io.swagger.config.ScannerFactory;
import io.swagger.jaxrs.config.BeanConfig;
import io.swagger.jaxrs.config.DefaultJaxrsScanner;
import io.swagger.jaxrs.config.SwaggerContextService;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.jboss.logging.Logger;
public class SwaggerServlet extends HttpServlet {
private static Logger log = Logger.getLogger(SwaggerServlet.class);
@Override
public void init(ServletConfig config) throws ServletException {
try {
super.init(config);
BeanConfig beanConfig = new BeanConfig();
beanConfig.setVersion("1.0.0");
beanConfig.setSchemes(new String[] { "http" });
beanConfig.setHost("localhost:8080");
beanConfig.setBasePath("/api");
beanConfig.setResourcePackage("io.swagger.resources,io.swagger.jaxrs.listing,my.example.app.exampleresources");
beanConfig.setScan(true);
log.info("Swagger servlet has been initialized");
} catch (ServletException e) {
log.error(e);
}
}
}
在启动我的应用程序时,servlet似乎初始化:
09:40:33,809 INFO [my.example.app.examples.web.SwaggerServlet] (SwaggerServlet.java:40) Swagger servlet has been initialized
我的网络资源正常运作并正确响应(在 http://localhost:8080/app/example/listOfExampleResources.json 上)。 ExampleModel类只是一个POJO:
@Api(value = "/app")
@Stateless
@Path("/app")
@Consumes({ "application/json" })
@Produces({ "application/json" })
public class ExampleController {
@GET
@Path("/example/listOfExampleResources.json")
@ApiOperation(value = "Get list of example resources",
notes = "Note test",
response = ExampleModel.class,
responseContainer = "list"
)
@Produces("application/json")
public List<ExampleModel> getList() {
List<ExampleModel> list = new ArrayList<ExampleModel>();
ExampleModel example = new ExampleModel();
example.setName("Hello");
list.add(example);
return list;
}
}
我尝试过其他参数和配置方法,例如通过自定义Application类进行配置,在web.xml中定义servlet,提供手动资源。 我也用旧版本(3.0.4)取代了RestEasy - 但总而言之,我仍然得到404.
您是否知道如何获取 swagger.json 资源?
感谢您的时间。