这是我的代码:我从application.properties文件中获取所有值 SwaggerConfig.java
@Configuration
@EnableSwagger2
@Profile("!prod")
@PropertySource(value = { "classpath:application.properties" })
public class SwaggerConfig {
@Value("${swagger.api.title}")
private String title;
@Value("${swagger.api.description}")
private String description;
@Value("${swagger.api.termsOfServiceUrl}")
private String termsOfServiceUrl;
@Value("${swagger.api.version}")
private String version;
@Value("${swagger.api.controller.basepackage}")
private String basePackage;
@Bean
public Docket postMatchApi() {
return new Docket(DocumentationType.SWAGGER_2).select().apis(RequestHandlerSelectors.basePackage(basePackage))
.paths(PathSelectors.ant("/**")).build().apiInfo(metaData());
}
private ApiInfo metaData() {
return new ApiInfoBuilder().title(title).description(description).termsOfServiceUrl(termsOfServiceUrl)
.version(version).build();
}
这是我的springboot初始化程序:
@SpringBootApplication
@ComponentScan(basePackages = { "com.example.demo" })
@ComponentScan(basePackageClasses = {AppInitializer.class, SwaggerConfig.class})
@EnableAsync
@EnableRetry
public class AppInitializer{
public static void main(String[] args) {
SpringApplication.run(AppInitializer.class, args);
}
}
ServletInitializer.java
public class ServletInitializer extends SpringBootServletInitializer implements WebApplicationInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(PostMatchAppInitializer.class);
}
}
日志显示已映射:
[INFO ] 2018-01-17 16:46:37.055 [restartedMain] o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[],methods=[POST],consumes=[application/json],produces=[application/json]}" onto public <T> org.springframework.http.ResponseEntity<?> com.,org.springframework.validation.BindingResult) throws java.lang.Exception
[INFO ] 2018-01-17 16:46:37.055 [restartedMain] o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/v2/api-docs],methods=[GET],produces=[application/json || application/hal+json]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)
[INFO ] 2018-01-17 16:46:37.055 [restartedMain] o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/swagger-resources/configuration/ui]}" onto org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()
[INFO ] 2018-01-17 16:46:37.055 [restartedMain] o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/swagger-resources]}" onto org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()
[INFO ] 2018-01-17 16:46:37.055 [restartedMain] o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/swagger-resources/configuration/security]}" onto org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()
[INFO ] 2018-01-17 16:46:37.055 [restartedMain] o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
[INFO ] 2018-01-17 16:46:37.071 [restartedMain] o.s.w.s.m.m.a.RequestMappingHandlerMapping - Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
[INFO ] 2018-01-17 16:46:37.227 [restartedMain] o.s.w.s.m.m.a.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5e89f6: startup date [Wed Jan 17 16:46:34 CST 2018]; root of context hierarchy
这是我得到的错误:
[WARN ] 2018-01-17 16:46:42.217 [http-nio-8082-exec-1] o.s.w.s.PageNotFound - No mapping found for HTTP request with URI [/example/swagger-ui.html] in DispatcherServlet with name 'dispatcherServlet'
答案 0 :(得分:10)
对于新的Springfox版本(3.0.0),您需要做一些不同的事情
在pom.xml中添加以下依赖项
*
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
而不是两个
<artifactId>springfox-swagger2</artifactId>
和
<artifactId>springfox-swagger-ui</artifactId>
并访问../swagger-ui/而不是../ swagger-ui.html
答案 1 :(得分:4)
我遇到了同样的问题。所以基本上如果你使用 spring 3 或更多然后打开 swagger 页面,你只需要做 3 件事。
在 pom.xml 中添加 3 个依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2 </artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
创建配置文件。
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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);
}
}
打开此链接以访问 API。
http://localhost:8080/swagger-ui/
现在只需匹配您错过的步骤。一定要问你是否卡在某个地方。
答案 2 :(得分:2)
我发现问题是什么,在其中一个配置文件中我以某种方式有@EnableMvc注释,因为dispatcherservlet正在寻找映射/example/swagger-ui.html,因为它找不到它正在抱怨“找不到映射”。
删除@EnableMvc后,它工作正常。
答案 3 :(得分:1)
对于像我这样仍然出现“ Whitelabel”页面错误的其他人,请检查您是否具有:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.8.0</version>
</dependency>
在您的pom.xml
文件中,不仅是官方文档页面上显示的“ springfox-swagger2”依赖项,您还需要“ springfox-swagger-ui”。
答案 4 :(得分:1)
我英文不好,这就是GoogleTranslate的原因。
该版本和执行方法已经过时了,如果您希望自动生成文档,SpringDoc会基于OpenAPI 3规范针对Spring Boot 1.x和Java简化API文档的生成和维护。 2.x。应用程序。
要使魔术发生,我们只需将依赖项添加到pom中:
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.2.32</version>
</dependency>
然后访问http://localhost:8080/v3/api-docs/已有的描述
并大摇大摆:http://localhost:8080/swagger-ui.html
仅此而已。
更多detail
如果您想自定义api信息,则可以包含Java样式注释:
@OpenAPIDefinition(
info = @Info(
title = "API personas",
description = "Este es un ejemplo de servidor Personas-Server."
+ "Usted puyede encontrar mas acerca de Swagger " ++"[http://swagger.io](http://swagger.io) o en "
+ "[irc.freenode.net, #swagger](http://swagger.io/irc/).",
termsOfService = "http://swagger.io/terms/",
license = @License(
name = "Apache 2.0",
url = "http://springdoc.org"),
version = "otra"
))
@Tag(name = "persona", description = "API para personas")
@RestController
@RequestMapping("persona")
public class PersonaRest extends GeneralRest {}
也可以通过特殊方法生成:
@Operation(
summary = "traer todas las personas",
description = "api para traer todas las personas, aqui no se tienen en cuenta paginaciones, ni filtros, trae todos los registros",
tags = { "persona" }
)
@ApiResponses(
value = {
@ApiResponse(
responseCode = "200",
description = "Operación exitosa",
content = @Content(
mediaType = "application/json",
array = @ArraySchema(
schema = @Schema(
implementation = PersonaTO.class
)))),
@ApiResponse(
responseCode = "401",
description = "Sin autorización",
content = @Content(
mediaType = "application/json",
schema = @Schema(
implementation = Object.class
))),
})
@GetMapping
public List personas() {
return personaServicio.obtenerTodo();
}
使用最新的库和包含项始终是一种好习惯。
答案 5 :(得分:1)
即使添加适当的依赖项后仍遇到问题 然后按照以下步骤
1。转到C:\ Users \ User.m2
2.删除存储库文件夹(完整文件夹删除,即Shift + Delete按钮窗口)
此文件夹基本上包含您的项目需要的所有jar 因此,当您再次打开项目时,它将自动下载依赖项
答案 6 :(得分:0)
将敏捷配置移至您的SpringBootApplication
类。那将解决whitable页面错误。
答案 7 :(得分:0)
首先,在spring boot pom文件中添加以下依赖项,然后在应用程序类上添加@EnableSwagger批注,然后添加webappconfig类
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.1</version>
</dependency>
@Configuration
@EnableWebMvc
public class ApplicationWebMvcConfig implements WebMvcConfigurer {.
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}。
答案 8 :(得分:0)
对我来说,它是摇摇欲坠的依赖版本。
问题
spring boot - 2.3.4
java - 8
swagger - 3.0.0
没问题
spring boot - 2.3.4
java - 8
swagger - 2.9.2
答案 9 :(得分:0)
对于SpringBoot Ver:2.4.0-M4,我建议进行以下设置。 确保使用SpringFox ver:3.0.0
//build.gradle file
def springFoxVer = '3.0.0'
def log4jVer = '2.13.3'
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-data-rest'
implementation 'org.springframework.boot:spring-boot-starter-hateoas'
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.data:spring-data-rest-hal-explorer'
implementation "io.springfox:springfox-swagger2:$springFoxVer"
implementation "io.springfox:springfox-boot-starter:$springFoxVer"
implementation "io.springfox:springfox-swagger-ui:$springFoxVer"
implementation "io.springfox:springfox-data-rest:$springFoxVer"
implementation "org.apache.logging.log4j:log4j-core:$log4jVer"
implementation "org.apache.logging.log4j:log4j-api:$log4jVer"
implementation 'org.springframework.boot:spring-boot-starter-actuator'
compileOnly 'org.projectlombok:lombok'
developmentOnly 'org.springframework.boot:spring-boot-devtools'
runtimeOnly 'com.h2database:h2'
annotationProcessor 'org.projectlombok:lombok'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
在配置类中:
@Configuration
public class SwaggerDocumentationConfig {
ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Sample Indentity in Project")
.description("The identity API provides standardized mechanism for identity management such as creation, update, retrieval, deletion. Party can be an individual or an organization that has any kind of relation with the enterprise. ### Resources - Individual Party API performs the following operations : - Retrieve an individual - Retrieve a collection of individuals according to given criteria - Create a new individual - Update an existing individual - Delete an existing individual")
.license("")
.licenseUrl("http://unlicense.org")
.termsOfServiceUrl("")
.version("1.0.0")
.contact(new Contact("Sean Huni", "https://sean-huni.xyz", "sean2kay@gmail.com"))
.build();
}
@Bean
public Docket customImplementation() {
return new Docket(DocumentationType.SWAGGER_2)
.tags(new Tag("Person Entity", "Repository for People's entities"))
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(apiInfo());
}
}
在应用程序运行器/执行器类中:
@SpringBootApplication
@EnableSwagger2
@EnableJpaRepositories
@Import(SpringDataRestConfiguration.class)
public class SpringSwaggerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringSwaggerApplication.class, args);
}
}
As instructed here。大多数情况下,只是花时间阅读要求和设置过程就意味着可以节省一天的麻烦。
答案 10 :(得分:0)
就我而言,我从 swagger 2.7.0 升级到 3.0.0,这些是到达蜜罐的步骤:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
@SpringBootApplication(scanBasePackages = {"com.acme.product.server"})
@RestController
@EnableScheduling
@EnableSwagger2
public class AcmeProductServer {
// [...]
// --- swagger integration
@Bean
public Docket productApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.acme.product.server"))
.build();
}
}
然后我真的不得不删除并刷新我在“C:\Users\Zaphod.m2\repository”的本地 maven 存储库
最后一个问题是使用不同的 URL:http://localhost:8080/iam/swagger-ui/(末尾没有斜杠或“/index.html”,它不起作用)< /p>
答案 11 :(得分:0)
确保以下内容与版本一起:
添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
@Bean Dokcet 定义在@SpringBootApplication 类中
swagger ui 端点 http://localhost:<port-number>/swagger-ui
P.S:在 spring boot (v2.5.0) 中测试