[英]Added Springfox Swagger-UI and it's not working, what am I missing?
按照此處的說明進行操作:
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"
並像這樣配置 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();
}
}
但是 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 unexpected error (type=Method Not Allowed, status=405).
Request method 'GET' not supported
在日志上我看到:
2017-09-11 09:54:31.020 WARN 15688 --- [nio-8080-exec-6] o.s.web.servlet.PageNotFound : Request method 'GET' not supported
2017-09-11 09:54:31.020 WARN 15688 --- [nio-8080-exec-6] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by Handler execution: org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'GET' not supported
http://localhost:8080/swagger-resources返回:
[{"name": "default",
"location": "/v2/api-docs",
"swaggerVersion": "2.0"}]
我錯過了什么?
我嘗試了大多數這些答案,最終的解決方案是爬行..
正確的網址如下
http://localhost:8080/swagger-ui/
我正在使用 Springfox swagger-ui 3.xx
請參閱完整的招搖設置: http : //muralitechblog.com/swagger-rest-api-dcoumentation-for-spring-boot/
已經有很多答案給出了正確的答案,但仍然存在一些關於錯誤的混淆。
如果你使用的是Spring Boot Version >= 2.2,建議使用SpringFox Swagger 3.0.0版
現在,只需要在 pom.xml 中添加一個依賴項。
<!-- Swagger dependency -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
應用程序啟動后,您可以通過點擊任一新的 swagger URL 來獲取文檔。
選項 1:http://localhost:8080/swagger-ui/
選項 2:http://localhost:8080/swagger-ui/index.html
經過許多解決方案后,沒有任何效果。 但
版本 - V2 || io.springfox <= 2.X.0
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-schema</artifactId>
<version>2.9.2</version>
</dependency>
V2 瀏覽器 URL -> http://localhost:8080/swagger-ui.html
版本 - V3 || io.springfox >= 3.0.0
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
V3 瀏覽器 URL -> http://localhost:8080/swagger-ui/#/
Mvn clean
@Configuration
@EnableSwagger2
無需為簡單的 swagger 配置(即路徑和基礎包)添加新的 Docket Bean
我遇到了這個問題,因為我的端點具有具有以下形式的路徑變量的請求映射:/{var}。 事實證明,這對於 GET 和 POST 端點都是一個問題,即 GET /{var} 和 POST /{var} 塊 swagger-ui。 一旦我使路徑更加具體,我就可以使用 swagger-ui 來工作。
引自https://github.com/springfox/springfox/issues/1672
當 spring 找到一個只有一個變量 swagger 的簡單路徑時,無法攔截 URL。
通過調查評論中的各種想法發現。
對於 Spring 版本 >= 2.2,你應該添加依賴 springfox-boot-starter
pom.xml:
<properties>
<java.version>1.8</java.version>
<io.springfox.version>3.0.0</io.springfox.version>
</properties>
<dependencies>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${io.springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${io.springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-data-rest</artifactId>
<version>${io.springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-bean-validators</artifactId>
<version>${io.springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>${io.springfox.version}</version>
</dependency>
</dependencies>
應用程序SwaggerConfig
@Configuration
@EnableSwagger2
public class ApplicationSwaggerConfig {
@Bean
public Docket employeeApi() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
Swagger-UI 鏈接: http://localhost:8080/swagger-ui/index.html#/
對於 3.0.0 版本,只有一個依賴項:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
之后,您可以訪問 swagger-ui:
http://localhost:8080/swagger-ui/#
http://localhost:8080/swagger-ui/index.html
對於 2.xx 版
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${io.springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${io.springfox.version}</version>
</dependency>
訪問 swagger-ui: http://localhost:8080/swagger-ui
如果您使用的是 Spring Boot 版本 >= 2.2,我建議您使用 SpringFox Swagger 版本 3.0.0。 保持你的 pom.xml 依賴配置如下:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
保持你的 Swagger 配置類如下:
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
@Configuration
@EnableSwagger2
public class SwaggerConfig {
public static final Contact DEFAULT_CONTACT = new Contact(
"Sample App", "http://www.sample.com", "sample@gmail.com");
public static final ApiInfo DEFAULT_API_INFO = new ApiInfo(
"Awesome API Title", "Awesome API Description", "1.0",
"urn:tos", DEFAULT_CONTACT,
"Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", Arrays.asList());
private static final Set<String> DEFAULT_PRODUCES_AND_CONSUMES =
new HashSet<String>(Arrays.asList("application/json",
"application/xml"));
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(DEFAULT_API_INFO)
.produces(DEFAULT_PRODUCES_AND_CONSUMES)
.consumes(DEFAULT_PRODUCES_AND_CONSUMES);
}
}
現在,通過以下 URL 訪問您的 swagger UI:http://localhost:8080/swagger-ui/index.html#/
我也遇到了這個問題,問題是我們有一個沒有路徑映射的控制器(因此映射到“/”)。 這阻止了對 swagger-ui 資源的請求。
我最近有類似的問題
http://localhost:8080/swagger-ui/ - 沒有用
http://localhost:8080/swagger-ui/index.html - 工作正常
...最后這個問題是由我的 pom.xml 中的一些混亂引起的
<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>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
我刪除了前兩個依賴項,所以只剩下 springfox-boot-starter,經過幾次 maven 清理並重新啟動應用程序,第一個路徑也開始正常工作 http://localhost:8080/swagger-ui/
在控制器級別( @RestController\\@Controller
注釋之后@RequestMapping("/")
添加@RequestMapping("/")
可以幫助我擺脫Request method 'GET' not supported
問題。 感謝 Dhermanns 的建議
在控制器級別添加@RequestMapping("/")
,它有效。
結論:我發現 maven 存儲庫${user_home}/.m2/repository/io/springfox/springfox-swagger-ui/2.9.2
下沒有 jar 文件,經過以下步驟后一切正常。
我按照以下步驟解決了這個問題:
${user_home}/.m2/repository/io/springfox/springfox-swagger-ui/2.9.2
reimport all maven projects
我的spring boot版本是2.2.2。
我遇到了 swagger 問題/swagger-ui.html 不支持請求方法“get”\\不支持請求方法“get”。\\supported methods post
我能夠解決這個問題
在我的控制器 api @RequestMapping() 中沒有路徑信息。 提供的路徑如下修復 - @RequestMapping(value = '/v1/createAnalytic')
io.springfox >= 3,並且還使用 SpringSecurity
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
SpringFoxConfig 類
@Configuration
@EnableSwagger2
public class SpringFoxConfig implements WebMvcConfigurer {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build()
.apiInfo(getApiInfo());
}
private ApiInfo getApiInfo() {
return new ApiInfo(
"company_name",
"message here",
"VERSION_1",
"TERMS OF SERVICE URL",
new Contact("company", "url", "EMAIL"),
"LICENSE",
"LICENSE URL",
Collections.emptyList()
);
}
}
WebConfig 類(確保不使用@EnableWebMvc注釋,否則會遇到錯誤)
@Configuration
//@EnableWebMvc
public class WebConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**")
.allowedMethods("HEAD", "GET", "POST", "PUT", "DELETE", "PATCH",
"OPTIONS");
}
}
安全配置類
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private static final String[] AUTH_WHITELIST = {
// -- Swagger UI v2
"/v2/api-docs",
"/swagger-resources",
"/swagger-resources/**",
"/configuration/ui",
"/configuration/**",
"/configuration/security",
"/swagger-ui.html",
"/webjars/**",
// -- Swagger UI v3 (OpenAPI)
"/v3/api-docs/**",
"/swagger-ui/**",
"/swagger-ui/",
"/swagger-ui"
// other public endpoints of your API may be appended to this array
@Override
protected void configure(HttpSecurity httpSecurity) throws Exception{
httpSecurity.cors();
httpSecurity.csrf().disable();
httpSecurity.sessionManagement()
.sessionCreationPolicy(SessionCreationPolicy.STATELESS)
.and()
.authorizeRequests()
.antMatchers(AUTH_WHITELIST).permitAll()
.anyRequest()
.authenticated();
httpSecurity.addFilterBefore(jwtRequestFilter,
UsernamePasswordAuthenticationFilter.class);
}
};
如果您使用版本 - V3 || io.springfox >= 3.0.0
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
Java代碼
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2).select()
.apis(RequestHandlerSelectors.basePackage("Your Controller package name"))
.paths(PathSelectors.any()).build();
}
}
V3 瀏覽器 URL -> http://localhost:8080/swagger-ui/#/
運行(必備):Mvn clean
我試圖在單個文件@EnableWebMvc
Swagger @Configuration
類與@EnableWebMvc
類結合起來。
不工作:
@Configuration
@EnableSwagger2
@EnableWebMvc
public class SwaggerConfiguration extends WebMvcConfigurerAdapter {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/swagger-ui/**")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
解決方案是在 2 個獨立的 Java 類中制作它,例如在文檔中:
@Configuration
@EnableSwagger2
public class SwaggerConfiguration {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
}
@Configuration
@EnableWebMvc
public class WebAppConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
}
}
嘗試重新啟動您的 IDE。
在嘗試了許多這些建議但仍然沒有任何運氣之后,我看到了這篇博文: https : //medium.com/swlh/openapi-swagger-ui-codegen-with-spring-boot-1afb1c0a570e
作者指出,“注意:如果您收到白標錯誤頁面,請嘗試重新啟動 IDE 並再次運行該項目。”
這就像一個魅力。
這條評論節省了我很多時間。 簡而言之 - 我發現我的項目中有人將映射添加到控制器,如下所示:
@RestController("/api/test")
如果要使用 3.0 版,還必須在 Docket Constructor 中將 DocumentationType 更改為OAS_30 :
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30)
.select()
.apis(RequestHandlerSelectors.any())
.paths(PathSelectors.any())
.build();
}
對我來說,這是一個問題,因為現有的 API
我在控制器上有一個Existing API
,就像
http://localhost:8080/{PathParam}
我把它改成
http://localhost:8080/domain/{PathParam}
問題解決了!!!
必須做兩件事來修復它:
添加了以下依賴項並刪除了其他 swagger 依賴項
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
pom.xml 中的 org.springframework.security 依賴項阻止了 swagger-ui,因此添加了以下代碼以繞過 swagger UI 的安全性:
@Configuration public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter { @Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/v2/api-docs", "/swagger-resources/**", "/swagger-ui/**"); } }
我只是重新加載了我的 maven,它對我有用我的 POM 文件中有以下依賴項。 ` 4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.4 com.example demo 0.0.1-SNAPSHOT demo Spring Boot 的演示項目 <java.version>1.8</java.version>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.29</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
`
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.