今天想通過swagger測試接口啟動服務(wù)后卻跳出
瀏覽器跳出的窗口
通過網(wǎng)上查詢有的說直接把@EnableSwagger2注解加在主啟動類就可以了,我試了下的確可以打開swagger的頁面,但是這樣會掃到使用的框架的接口,所以這種方法應(yīng)該是不正確的。
最后發(fā)現(xiàn)原因應(yīng)該是和Spring沒有掃描到Swagger配置類從而沒法自動創(chuàng)建Bean:
@Configuration
@EnableSwagger2
public class Swagger2Configuration {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.***"))
.paths(PathSelectors.any())
.build();
} .....配置類我只截了一部分
解決方法:主啟動類加上@ComponentScan("swagger配置類所在包")以保證配置類被掃描到
@ComponentScan("com.hello.cms.config")//根據(jù)自己需要填寫包名
@SpringBootApplication
public class ManagerCmsApplication {
public static void main(String[] args) {
SpringApplication.run(ManagerCmsApplication.class);
}
}
如果加上注解依然沒用可以刷新幾次瀏覽器(我這里測試有效)
大家有好的建議請留言,我是個java自學(xué)的小白,還望多多包涵
2019/02/24