[英]Swagger UI not working Rest APi
我有一個Rest Api項目,我正在嘗試使用招搖工具來編寫文檔和UI。 我能夠獲得該文檔,但UI沒有看到api文檔,因此空白。 我已經在webapps / WEB-INF下復制了dist文件夾的內容,我可以使用以下文檔來訪問文檔: http:// localhost:8080 / ml-api-ws / v2 / api-docs我試圖將index.html中的url更改為指向此文檔,但大張旗鼓地把空白抬高。
webapps / WEB-INF:
空白招搖
<script type="text/javascript">
$(function () {
var url = window.location.search.match(/url=([^&]+)/);
if (url && url.length > 1) {
url = decodeURIComponent(url[1]);
} else {
//url = "http://petstore.swagger.io/v2/swagger.json";
url = "http://localhost:8080/ml-api-ws/v2/api-docs";
}
Pom.xml
<!-- SpringFox Dependency For Swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.4.0</version>
</dependency>
<!-- Swagger UI Depedency -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.0.2</version>
</dependency>
servlet.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.epsilon.al.ml.controller" />
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/webjars/"/>
<mvc:default-servlet-handler/>
<mvc:annotation-driven />
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="swagger2Config" class = "springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" />
<!-- <bean name="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewResolver"
/> <bean name="jsonTemplate" class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"
/> -->
</beans>
Web.xml:
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>MlApi</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:*-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>ml-rest-api</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ml-rest-api</servlet-name>
<!-- <url-pattern>/ml-api-ws/*</url-pattern> -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- <welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list> -->
<welcome-file-list>
<welcome-file>/WEB-INF/index.html</welcome-file>
</welcome-file-list>
</web-app>
我還嘗試將api文檔保存在.json文件中,並使用swagger-ui進行瀏覽,但是該頁面沒有執行任何操作。
謝謝!!
我能夠解決此問題,但是在webapps下創建了一個文件夾,並在其中移動了dist文件夾的內容。 一旦有了這個,我便在servlet.xml中添加了index.html的映射,並能夠訪問api的映射。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.