java Swagger UI 未在 index.html 页面上显示任何内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30334873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swagger UI Not Displaying Any Content On index.html Page
提问by risingTide
UPDATED with SOLUTION below!!!
//////////////////////////////////////////////////////////////
Thanks to the advice of Ron below I slightly modified my setup to use BeanConfig instead of SwaggerConfig and got this working. In order to do this I had to modify the servlet and also (and this is where I believe the missing piece was) add the BeanConfig entry into the spring application context file so that spring picks up the resources. I included the updates below with comments in my code showing the old and new/updated code. It's possible I could've continued with the SwaggerConfig (maybe I was just missing something in the spring application context file for that as well?) but the BeanConfig works so I'm going to leave it as is.
//////////////////////////////////////////////////////////////
更新了下面的解决方案!!!
////////////////////////////////////////////////// ////////////
感谢下面 Ron 的建议,我稍微修改了我的设置以使用 BeanConfig 而不是 SwaggerConfig 并使其正常工作。为了做到这一点,我必须修改 servlet,并且(我认为这是缺失的部分)将 BeanConfig 条目添加到 spring 应用程序上下文文件中,以便 spring 获取资源。我在我的代码中包含了下面的更新和注释,显示了旧的和新的/更新的代码。有可能我可以继续使用 SwaggerConfig(也许我只是在 spring 应用程序上下文文件中缺少一些东西?)但是 BeanConfig 可以工作,所以我将保持原样。
////////////////////////////////////////////////// ///////////
I'm trying to get Swagger running with my local REST-based Java App and have made quite a bit of progress. However, I seem to be missing something easy when I'm trying to get the Swagger UI working.
我正在尝试让 Swagger 与我本地的基于 REST 的 Java 应用程序一起运行,并且取得了相当大的进展。然而,当我试图让 Swagger UI 工作时,我似乎错过了一些简单的东西。
Whenever I actually hit this address: http://localhost:9082/mbl/index.htmlI'm getting the little green swagger header at the top but a blank white body with no content underneath. Shouldn't I be seeing more than this in the body of the page?
每当我真正点击这个地址时: http://localhost:9082/mbl/index.html我在顶部看到一个小小的绿色 swagger 标题,但下面是一个空白的白色主体,下面没有任何内容。我不应该在页面正文中看到更多内容吗?
My stack is this: Java 6 / Wink / Spring 3.1 / Hymanson 2.5 / JAX-RS (JSR-311) and I'm using the following Swagger jars: swagger-annotations-1.3.10.jar / swagger-core_2.10-1.3.10.jar / swagger-jaxrs_2.10-1.3.10.jar.
我的堆栈是这样的:Java 6 / Wink / Spring 3.1 / Hymanson 2.5 / JAX-RS (JSR-311),我正在使用以下 Swagger jar:swagger-annotations-1.3.10.jar / swagger-core_2.10- 1.3.10.jar / swagger-jaxrs_2.10-1.3.10.jar。
Now I've managed to get some json displaying that looks like this when i hit http://localhost:9082/mbl/services/api-docs:
现在,当我点击http://localhost:9082/mbl/services/api-docs时,我已经设法得到一些 json 显示,看起来像这样:
{"apiVersion":"1.0","swaggerVersion":"1.2","info":{"title":"Java API","description":"The following documentation contains the REST Service API useful for interacting with web services.","termsOfServiceUrl":"terms of service","contact":"[email protected]","license":"license type","licenseUrl":"license url"}}
I can see that this is being generated from my SwaggerServlet.java which looks like this:
我可以看到这是从我的 SwaggerServlet.java 生成的,如下所示:
package com.somewhere.mblsvc.web;
import...
public class SwaggerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/* additional working code */
BeanConfig beanConfig;
public void setBeanConfig(BeanConfig beanConfig) {
this.beanConfig = beanConfig;
}
/* end additional working code */
@Override
public void init(ServletConfig servletConfig) {
try {
/* code from original post*/
// SwaggerConfig swaggerConfig = new SwaggerConfig();
// ConfigFactory.setConfig(swaggerConfig);
// swaggerConfig.setBasePath("/mbl/services");
// swaggerConfig.setApiVersion("1.0");
// swaggerConfig.setApiInfo(new ApiInfo("Java API", "The following //documentation contains the REST Service API useful for interacting with web //services.", "terms of service", "[email protected]", "license type", "license //url"));
// ScannerFactory.setScanner(new DefaultJaxrsScanner());
// ClassReaders.setReader(new DefaultJaxrsApiReader());
/* end code from original post*/
/* updated working code */
beanConfig.setBasePath("/mbl/x-services");
beanConfig.setVersion("1.0");
beanConfig.setResourcePackage("com.somewhere.mblsvc.resources");
beanConfig.setScan(true);
/* end updated working code */
} catch (Exception e) {
e.printStackTrace();
}
}
}
Also, I have the following in my spring application context xml file:
另外,我的 spring 应用程序上下文 xml 文件中有以下内容:
<bean class="org.apache.wink.spring.Registrar">
<property name="classes">
<set value-type="java.lang.Class">
</set>
</property>
<property name="instances">
<set>
<ref local="jaxbProvider" />
<ref local="apiDeclarationProvider" />
<ref local="apiListingResourceJson" />
<ref local="resourceListingProvider" />
</set>
</property>
<!-- Hymanson Providers -->
<bean id="jaxbProvider" class="com.fasterxml.Hymanson.jaxrs.json.HymansonJaxbJsonProvider" >
<property name="mapper" ref="HymansonObjectMapper"/>
</bean>
<bean id="HymansonObjectMapper" class="com.fasterxml.Hymanson.databind.ObjectMapper" >
<property name="annotationIntrospector" ref="HymansonAnnotationIntrospector" />
</bean>
<bean id="HymansonAnnotationIntrospector" class="com.fasterxml.Hymanson.databind.introspect.AnnotationIntrospectorPair" >
<constructor-arg ref="primaryAnnotationIntrospector" />
<constructor-arg ref="secondaryAnnotationIntrospector" />
</bean>
<bean id="primaryAnnotationIntrospector" class="com.fasterxml.Hymanson.module.jaxb.JaxbAnnotationIntrospector" />
<bean id="secondaryAnnotationIntrospector" class="com.fasterxml.Hymanson.databind.introspect.HymansonAnnotationIntrospector" />
<!-- Swagger Configuration and Providers -->
<!-- additional working code -->
<bean id="beanConfig" class="com.wordnik.swagger.jaxrs.config.BeanConfig">
<property name="title" value="Java API"/>
<property name="version" value="1.0" />
<property name="basePath" value="/mbl/services"/>
<property name="resourcePackage" value="com.somewhere.mblsvc.resources"/>
<property name="scan" value="true"/>
</bean>
<!-- end additional working code -->
<bean id="apiDeclarationProvider" class="com.wordnik.swagger.jaxrs.listing.ApiDeclarationProvider" />
<bean id="apiListingResourceJson" class="com.wordnik.swagger.jaxrs.listing.ApiListingResourceJSON" />
<bean id="resourceListingProvider" class="com.wordnik.swagger.jaxrs.listing.ResourceListingProvider" />
My web.xml looks like this:
我的 web.xml 看起来像这样:
<!-- REST servlet that dispatches to the App (resource class). Remove Init params entry containing application file -->
<servlet>
<servlet-name>Wink Servlet</servlet-name>
<servlet-class>org.apache.wink.server.internal.servlet.RestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Wink Servlet</servlet-name>
<url-pattern>/services/*</url-pattern>
</servlet-mapping>
<!-- Enabling Swagger servlet -->
<servlet>
<servlet-name>Swagger Servlet</servlet-name>
<servlet-class>com.somewhere.mblsvc.web.SwaggerServlet</servlet-class>
<load-on-startup>-1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Swagger Servlet</servlet-name>
<url-pattern>/api-docs</url-pattern>
</servlet-mapping>
Here's a snippet of my Resource class:
这是我的 Resource 类的片段:
@Path("test")
@Api(value ="test", description="Test Services")
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public class TestResource {
.
.
.
@GET
@Path("testInfo")
@ApiParam(defaultValue="you would put test case input here for a post")
@ApiOperation(value="Composite service returning extensive test information", response=com.somewhere.mblsvc.messages.test.testinfo.pojo.UserResponseMessage.class)
@ApiResponses(value={
@ApiResponse(code=200, message="OK"),
@ApiResponse(code=500, message="Internal Error")
})
@JsonSerialize(include=JsonSerialize.Inclusion.ALWAYS)
public Response getTestInfo(@Context HttpHeaders headers,
@CookieParam(value = "testBrand") String testBrand) {
.
.
.
And, finally, the only important part of my index.html (that I can tell) looks like this:
最后,我的 index.html 唯一重要的部分(我可以说)如下所示:
<script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = url[1];
} else {
url = "http://" + window.location.hostname + (window.location.port ? ':'+ window.location.port: '') + "/mbl/services/api-docs";
}
.
.
.
I will gladlysupply any more information as needed. Does anyone have any idea what I might be missing?
我很乐意根据需要提供更多信息。有谁知道我可能会错过什么?
Thanks a lot!
非常感谢!
采纳答案by Ron
The core problem is that your resources are not really scanned. You do get the right now is the basic Swagger response, but if you look at the content, it has no API definitions in it.
核心问题是你的资源没有真正被扫描。您现在确实了解的是基本的 Swagger 响应,但如果您查看内容,就会发现其中没有 API 定义。
As a result, swagger-ui is unable to show anything, because there's nothing to show.
结果, swagger-ui 无法显示任何内容,因为没有任何内容可显示。
While I'm curious as to how you got to the configuration above, the truth is that the integration can be simpler. We don't have specific documentation for Wink (we should), but the idea is very similar to any of the JAX-RS integrations.
虽然我很好奇您是如何获得上述配置的,但事实是集成可以更简单。我们没有 Wink 的特定文档(我们应该有),但这个想法与任何 JAX-RS 集成都非常相似。
I'd recommend the following steps:
我建议执行以下步骤:
- Migrate to swagger-core 1.5. It's obvious this is a new integration and there's no reason not to use it.
- Check out the wink sample- should be reallyeasy to follow.
- Read the guidesfor 1.5 - they are not for wink, but they are all similar and you can infer to wink.
- Check the migration guideas it also contains details that may help you out.
- 迁移到 swagger-core 1.5。很明显,这是一个新的集成,没有理由不使用它。
- 查看眨眼示例- 应该很容易理解。
- 阅读1.5的指南- 它们不是为了眨眼,但它们都很相似,你可以推断出眨眼。
- 查看迁移指南,因为它也包含可能对您有所帮助的详细信息。
I realize this doesn't solve the question at-hand exactly, but I'd rather promote the right overall solution in this case.
我意识到这并不能完全解决手头的问题,但我宁愿在这种情况下推广正确的整体解决方案。