gpt4 book ai didi

java - Swagger UI 无法正常工作 Rest APi

转载 作者:行者123 更新时间:2023-12-02 03:39:07 25 4
gpt4 key购买 nike

我有一个 Rest Api 项目,我正在尝试使用 swagger 来记录文档和 UI。我能够获取文档,但 UI 没有看到 api 文档并且显示为空白。我已复制 webapps/WEB-INF 下 dist 文件夹的内容我可以使用以下方式访问文档:http://localhost:8080/ml-api-ws/v2/api-docs我尝试更改 index.html 中的 url 以指向此文档,但 swagger 却显示为空白。

webapps/WEB-INF:

enter image description here

空白招摇:

enter image description here索引.html:

  <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。

关于java - Swagger UI 无法正常工作 Rest APi,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37055447/

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