gpt4 book ai didi

java - Swagger 和 web.xml

转载 作者:行者123 更新时间:2023-12-01 10:54:29 25 4
gpt4 key购买 nike

我正在尝试使用我的 java Rest api 进行 swagger 工作。我正在关注swagger documentation 。然而,我却无法 Swagger 去工作。

例如,根据文档,我应该从 swagger 获取一个 json 文件 - 但无论我尝试什么 url,我都永远不会得到它。

<context-param>
<param-name>resteasy.providers</param-name>
<param-value>
io.swagger.jaxrs.listing.ApiListingResource,
io.swagger.jaxrs.listing.SwaggerSerializers,
com.my.service.rest.handler.JsonParseExceptionHandler,
com.my.service.rest.handler.IllegalArgumentExceptionHandler,
com.my.service.rest.handler.JsonMappingExceptionHandler,
com.my.service.rest.handler.RuntimeExceptionHandler
</param-value>
</context-param>

<servlet>
<servlet-name>resteasy-servlet</servlet-name>

<servlet-class>io.swagger.jaxrs.config.DefaultJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>0.1</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://localhost:8080/indicatorsService</param-value>

<!-- also not working <param-value>http://localhost:8080/docs</param-value> -->
</init-param>
</servlet>
<servlet>
<servlet-name>JbossServlet</servlet-name>
<servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>JbossServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

根据 swagger 文档,如果我转到 http://localhost:8080/indicatorsService/swagger.json ,我应该得到 swagger json - 相反,我得到以下错误

Error in handling REST request. ExpectionMessage: Could not find resource for relative : /swagger.json of full path: http://localhost:8080/indicatorsService/swagger.json: org.jboss.resteasy.spi.NotFoundException: Could not find resource for relative : /swagger.json of full path: http://localhost:8080/indicatorsService/swagger.json
at org.jboss.resteasy.core.registry.RootSegment.matchChildren(RootSegment.java:360) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:374) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.registry.RootSegment.matchRoot(RootSegment.java:367) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:307) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:173) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:118) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:208) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:55) [resteasy-jaxrs-2.3.2.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:50) [resteasy-jaxrs-2.3.2.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:329) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.jboss.weld.servlet.ConversationPropagationFilter.doFilter(ConversationPropagationFilter.java:62) [weld-core-1.1.5.AS71.Final.jar:2012-02-10 15:31]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:280) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:248) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:275) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:161) [jbossweb-7.0.13.Final.jar:]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.1.1.Final.jar:7.1.1.Final]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:153) [jboss-as-web-7.1.1.Final.jar:7.1.1.Final]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:155) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) [jbossweb-7.0.13.Final.jar:]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:368) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:877) [jbossweb-7.0.13.Final.jar:]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:671) [jbossweb-7.0.13.Final.jar:]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:930) [jbossweb-7.0.13.Final.jar:]
at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_85]

谢谢。

最佳答案

我的应用程序中有类似的配置,只不过我使用 Jersey 2 而不是 RestEasy。

我检查了我的 web.xml 文件,它的配置方式类似(servlet 类除外,它专门用于 Jersey 2)。以下是摘录:

<servlet>
<servlet-name>jax-rs-ws</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.jeff.MyJerseyApp</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>jax-rs-ws</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>swagger-servlet</servlet-name>
<servlet-class>com.wordnik.swagger.jersey.config.JerseyJaxrsConfig.JerseyJaxrsConfig</servlet-class>
<init-param>
<param-name>api.version</param-name>
<param-value>1.1.0</param-value>
</init-param>
<init-param>
<param-name>swagger.api.basepath</param-name>
<param-value>http://127.0.0.1:8080/myapp/rest</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>

这里有一些提示:

  • My Jersey servlet 映射到 /rest/* 模式。全局根 /* 也可能没问题,但让我们继续...
  • Swagger servlet 中定义的基本路径在我的应用程序中包含相同的 /rest URI。

此外,我必须使用以下 URL 来检索 JSON 结构的 API 信息:

http://127.0.0.1:8080/myapp/rest/api-docs

其 URI 以相同的 /rest 路径开头。这让我认为您应该使用以下地址:

http://localhost:8080/indicatorsService/api-docs

希望我猜对了......

关于java - Swagger 和 web.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33697430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com