swagger集成springmvc遇到Can't read swagger JSON from http://localhost:80 80/restful/api-docs
1条回答 默认 最新
你知我知皆知 2024-08-05 15:56关注以下回答参考 皆我百晓生、券券喵儿 等免费微信小程序相关内容作答,并由本人整理回复。
这个问题是由于Spring Boot和Swagger整合时配置的问题。你需要在你的Spring Boot应用的主类中添加以下依赖:
import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } }然后,在你的控制器或服务类上添加以下注解:
@ApiOperation(value = "Hello", notes = "This is a test") @Api(tags = "Test") @RequestMapping(path = "/test", method = RequestMethod.GET) public class TestController { // ... }这样,你就可以使用Swagger了。注意:这个例子假设你正在使用Spring Boot 2.1及以上版本。
另外,如果你想要从特定的端点获取API文档,请确保在你的路径选择器中正确地指定了路径(例如
/test)。解决 无用评论 打赏 举报