No mapping for GET /swagger-ui.html报错

访问地址:http://127.0.0.1:8080/swagger-ui.html

 控制台报错如下:

好吧,没有找到

回顾刚才的操作,小编在config文件夹里添加了Swagger2Config,启动找不到;

如何解决?

1.实现

implements WebMvcConfigurer

2.实现

addResourceHandlers

接口

/**
* Swagger2配置
* @author
*/
@Configuration
@EnableSwagger2
@EnableWebMvc
public class Swagger2Config implements WebMvcConfigurer {
	@Override
	public void addResourceHandlers(ResourceHandlerRegistry registry) {

			registry.addResourceHandler("/**").addResourceLocations(
					"classpath:/static/");
			registry.addResourceHandler("swagger-ui.html").addResourceLocations(
					"classpath:/META-INF/resources/");
			registry.addResourceHandler("/webjars/**").addResourceLocations(
					"classpath:/META-INF/resources/webjars/");
			WebMvcConfigurer.super.addResourceHandlers(registry);
	}

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
				.apis(RequestHandlerSelectors.basePackage("com.xxxx.xxx"))
				.paths(PathSelectors.any()).build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("API文档").version("1.0").build();
	}
}

 重新启动服务后访问:

  • 19
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
您在访问`/swagger-ui.html`时出现了`No mapping for GET`错误。这个错误通常是由于您的应用程序缺少相应的请求映射所引起的。 要解决这个问题,您可以尝试以下几个步骤: 1. 确保您的应用程序中已经正确配置了Swagger相关的依赖项。您可以在`pom.xml`文件中添加以下依赖项: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> ``` 或者,如果您使用Gradle构建工具,则可以在`build.gradle`文件中添加以下依赖项: ```groovy implementation 'io.springfox:springfox-swagger2:2.9.2' implementation 'io.springfox:springfox-swagger-ui:2.9.2' ``` 2. 确保您的应用程序中已经正确配置了Swagger的相关配置类。您可以创建一个继承自`WebMvcConfigurationSupport`的配置类,并重写`addResourceHandlers`方法,将Swagger的资源路径添加到资源处理程序中。例如: ```java @Configuration @EnableSwagger2 public class SwaggerConfig extends WebMvcConfigurationSupport { @Override protected void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("swagger-ui.html") .addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**") .addResourceLocations("classpath:/META-INF/resources/webjars/"); } } ``` 3. 确保您的应用程序中已经正确配置了Swagger的API文档。您可以创建一个Swagger配置类,并使用`@Api`和`@ApiOperation`等注解来定义API接口和操作。例如: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) .paths(PathSelectors.any()) .build(); } } ``` 请根据您的具体情况检查以上步骤,并确保正确配置了Swagger相关的依赖项、配置类和API文档。如果问题仍然存在,请提供更多详细信息以便我能够更好地帮助您解决问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值