Java 如何启用 Swagger UI?Tomcat、SpringMVC、REST
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27132766/
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
How to enable Swagger UI? Tomcat, SpringMVC, REST
提问by Dariusz Mydlarz
My configuration is as follow:
我的配置如下:
pom.xml
pom.xml
<dependencies>
<!-- Swagger -->
<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>0.9.1</version>
</dependency>
<!-- Swagger WebJar -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>2.0.24</version>
</dependency>
</dependencies>
root-context.xml
根上下文.xml
<mvc:annotation-driven/>
<beans:bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
I deploy my application into Tomcat 8.0. I am able to see Swagger JSON data at URI:
我将我的应用程序部署到 Tomcat 8.0。我可以在 URI 处看到 Swagger JSON 数据:
http://localhost:8080/myapp/api-docs
But I can't run Swagger UI. What more should I do to run Swagger UI in my project?
但我无法运行 Swagger UI。我还应该做什么才能在我的项目中运行 Swagger UI?
采纳答案by Dariusz Mydlarz
I solved my problem by adding swagger-ui resources to
我通过添加 swagger-ui 资源解决了我的问题
src\main\webapp\docs
I also added
我还加了
<mvc:default-servlet-handler/>
To pom.xml
到 pom.xml
Now I can access swagger UI under:
现在我可以通过以下方式访问 swagger UI:
http://localhost:8080/myapp/docs/
回答by leo
The trick is that your swagger-ui maven dependency is a webjar. You need to configure the path from your webserver to the webjar.
诀窍是您的 swagger-ui maven 依赖项是一个 webjar。您需要配置从 web 服务器到 webjar 的路径。
I used the current maven dependency:
我使用了当前的 maven 依赖项:
<dependency>
<groupId>org.webjars</groupId>
<artifactId>swagger-ui</artifactId>
<version>2.1.3</version>
</dependency>
And configured it with annotations:
并使用注释对其进行配置:
@Configuration
@EnableWebMvc
public class RestConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/swagger-ui/2.1.3/");
}
// [...]
}
Same can be done with with xml in your spring configuration:
同样可以在你的 spring 配置中使用 xml 来完成:
<mvc:resources mapping="/swagger-ui/**" location="classpath:/META-INF/resources/webjars/swagger-ui/2.1.3/"/>
Also see: http://www.jamesward.com/2012/04/30/webjars-in-spring-mvc
另见:http: //www.jamesward.com/2012/04/30/webjars-in-spring-mvc