按照此处的说明操作:
http://www.baeldung.com/swagger-2-documentation-for-spring-rest-api
我将这些依赖项添加到我的项目中:
compile "io.springfox:springfox-swagger2:2.7.0"
compile "io.springfox:springfox-swagger-ui:2.7.0"
Run Code Online (Sandbox Code Playgroud)
并像这样配置 SpringFox Swagger:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Run Code Online (Sandbox Code Playgroud)
但是 Swagger UI 似乎没有启用。我试过:
我得到的只是:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Sep 11 09:43:46 BST 2017
There was an …Run Code Online (Sandbox Code Playgroud) 我的配置如下:
的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>
Run Code Online (Sandbox Code Playgroud)
根的context.xml
<mvc:annotation-driven/>
<beans:bean class="com.mangofactory.swagger.configuration.SpringSwaggerConfig" />
Run Code Online (Sandbox Code Playgroud)
我将我的应用程序部署到Tomcat 8.0中.我能够在URI上看到Swagger JSON数据:
http://localhost:8080/myapp/api-docs
Run Code Online (Sandbox Code Playgroud)
但我无法运行Swagger UI.我还应该在项目中运行Swagger UI?
关于在Spring MVC中集成Swagger:
Swagger没有显示GET/PUT/POST文档@RequestMapping
在我的Spring MVC Rest webservice应用程序中,我有一个Login控制器和一个Student Controller.我刚刚配置了Swagger来生成Rest API文档.参考:http://java.dzone.com/articles/how-configure-swagger-generate
问:然而,Swagger只显示类级路径,我猜它不是显示类级别的@RequestMapping.,方法级别映射被忽略.有什么理由吗?
@Controller
@RequestMapping(value = "/login")
public class LoginController {
@RestController
@RequestMapping(value = "/students/")
public class StudentController {
@RequestMapping(value = "{departmentID}", method = RequestMethod.GET)
public MyResult getStudents(@PathVariable String departmentID) {
// code
}
@RequestMapping(value = "student", method = RequestMethod.GET)
public MyResult getStudentInfo(
@RequestParam(value = "studentID") String studentID,
@RequestParam(value = "studentName") String studentName) {
//code
}
@RequestMapping(value = "student", method = RequestMethod.POST) …Run Code Online (Sandbox Code Playgroud) 我正在尝试将 Springfox Swagger 2 与非 SpringBoot Spring 应用程序(Spring-web MVC)一起使用,使用 Spring-web 4.3.22-RELEASE。但是,当我包含以下依赖项时
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在我的 pom.xml 中,如果没有添加任何代码来使用它或类似的内容,我的“maven install”就会开始失败。我看到的错误是。
Caused by: java.lang.NoClassDefFoundError: org/springframework/context/event/EventListenerFactory
Caused by: java.lang.ClassNotFoundException: org.springframework.context.event.EventListenerFactory
Run Code Online (Sandbox Code Playgroud)
我认为这是一个 Spring 5 类,我不知道如何让它与这个旧版本一起工作。我尝试过的事情:
注意:我有在 Spring MVC 应用程序中实现 Swagger 的“简单”方法中提到的 Jackson Databind 依赖项
POM 的相关部分(很难发布整个 POM,因为我们有一个复杂的层次结构,对格式感到抱歉)
<properties>
<jackson.databind.version>2.9.8</jackson.databind.version>
<spring.version>4.3.22.RELEASE</spring.version>
</properties>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency> …Run Code Online (Sandbox Code Playgroud) swagger ×4
spring ×3
java ×2
spring-mvc ×2
springfox ×2
swagger-ui ×2
spring-boot ×1
swagger-2.0 ×1
tomcat ×1