添加了Springfox Swagger用户界面和it';它不工作了,我还缺什么?
按照此处的说明进行操作: 我将这些依赖项添加到我的项目中:添加了Springfox Swagger用户界面和it';它不工作了,我还缺什么?,spring,spring-boot,swagger,swagger-ui,springfox,Spring,Spring Boot,Swagger,Swagger Ui,Springfox,按照此处的说明进行操作: 我将这些依赖项添加到我的项目中: compile "io.springfox:springfox-swagger2:2.7.0" compile "io.springfox:springfox-swagger-ui:2.7.0" 配置SpringFox的招摇过市方式如下: import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.
compile "io.springfox:springfox-swagger2:2.7.0"
compile "io.springfox:springfox-swagger-ui:2.7.0"
配置SpringFox的招摇过市方式如下:
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();
}
}
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
但招摇过市的用户界面似乎没有启用。我试过:
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
返回:
[{"name": "default",
"location": "/v2/api-docs",
"swaggerVersion": "2.0"}]
我错过了什么
我遇到了这个问题,因为我的端点具有请求映射,路径变量的形式为:/{var}。事实证明,这对于GET和POST端点都是一个问题,即GET/{var}和POST/{var}块炫耀ui。一旦我把路径弄得更具体,我就可以使用大摇大摆的ui了
引自
当spring找到只有一个变量的简单路径时,swagger无法截获URL
通过调查评论中的各种想法发现。我也遇到了这个问题,问题是我们有一个没有路径映射的控制器(因此映射到“/”)。这阻止了对swagger ui资源的请求。在控制器级别(在
@RestController\@controller
注释之后)添加@RequestMapping(“/”
)有助于我摆脱请求方法“get”不受支持的问题 在控制器级别添加@RequestMapping(“/”
,它可以工作 结论:我发现maven repository${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
- 检查2.9.2下的文件是否完整。如果缺少文件,请删除整个2.9.2目录
- 在intellij idea中执行
重新导入所有maven项目
我的spring启动版本是2.2.2。我遇到了问题/swagger-ui.html请求方法'get'不受支持\请求方法'get'不受支持。\n受支持的方法发布
我能够解决这个问题
在我的控制器中,api@RequestMapping()没有路径信息。提供的路径如下Fix-@RequestMapping(value='/v1/createanalysis')我尝试了这些答案中的大多数,最终的解决方案是缓慢的
正确的URL如下所示
http://localhost:8080/swagger-用户界面/
我正在使用Springfox swagger ui 3.x.x
请参阅完整的摆动装置设置:
如果您使用的是Spring Boot版本>=2.2,我建议您使用SpringFox Swagger版本3.0.0。保持pom.xml依赖项配置如下:
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();
}
}
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
伊奥·斯普林福克斯
springfox启动程序
3.0.0
保持您的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);
}
}
import org.springframework.context.annotation.Bean;
导入org.springframework.context.annotation.Configuration;
导入springfox.documentation.service.apinfo;
导入springfox.documentation.service.Contact;
导入springfox.documentation.spi.DocumentationType;
导入springfox.documentation.spring.web.plugins.Docket;
导入springfox.documentation.swagger 2.annotations.enableSawagger 2;
导入java.util.array;
导入java.util.HashSet;
导入java.util.Set;
@配置
@使能招摇过市2
公共类招摇过市配置{
公共静态最终联系人默认\u联系人=新联系人(
“示例应用程序”http://www.sample.com", "sample@gmail.com");
公共静态最终ApiInfo默认值\u API\u INFO=新ApiInfo(
“Awesome API标题”、“Awesome API说明”、“1.0”,
“urn:tos”,默认联系人,
“Apache 2.0”http://www.apache.org/licenses/LICENSE-2.0,Arrays.asList());
私有静态最终设置默认值_生成_和_使用=
新的HashSet(Arrays.asList(“application/json”),
“应用程序/xml”);
@豆子
公开摘要api(){
返回新摘要(DocumentationType.SWAGGER_2)
.apinfo(默认的\u API\u信息)
.products(默认情况下,\产生,\消耗)
.consumes(默认的_生成_和_消费);
}
}
现在,通过转到以下URL访问您的招摇过市UI:http://localhost:8080/swagger-ui/index.html#/对于Spring版本>=2.2,您应该添加依赖项springfox启动程序
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>
招摇过市用户界面链接:http://localhost:8080/swagger-ui/index.html#/
io.springfox>=2.X
io.springfox>=3.X
经过多次解决后,一切都不起作用。但是
Version-V2 | | io.springfox已经有很多答案给出了正确的答案,但仍然存在一些关于错误的混淆
如果您使用的是Spring Boot版本>=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>
伊奥·斯普林福克斯
springfox启动程序
3.0.0
一旦应用程序启动,您可以通过点击任意一个新的招摇过市URL来获取文档
备选案文1:
http://localhost:8080/swagger-用户界面/
备选案文2:
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>
在:http://localhost:8080/swagger-ui
您是否有任何spring安全性可以阻止访问?@StanislavL:没有,我还没有启用安全性。@StanislavL:我添加了我收到的日志错误,它是一个PageNotFound。@Bean public Docket api(){返回新的Docket(DocumentationType.SWAGGER_2).groupName(“用户公共api”).select().api(RequestHandlerSelectors.any()).Path(PathSelectors.any()).build().pathMapping(“/”).EnableUrlTemplateing(false);}
这是我的工作配置。@StanislavL:我试过了,同样的错误。最后的/
是非常重要的部分。fg这是难以置信的。M
<!-- Swagger dependency -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<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>${io.springfox.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${io.springfox.version}</version>
</dependency>