1.打開swagger2網頁跳出"Unable to infer base url..."提示
原因:沒有掃描到Swagger配置類從而沒法自動創建Bean
解決方法:在啟動類上面添加@ComponentScan("swagger配置類所在包")注解
2.swagger出現No operations defined in spec!提示
原因:沒有掃描到到@Component(@Controller/@Service/@Repository)的類,啟動類注解@SpringBootApplication = (默認屬性)@Configuration + @EnableAutoConfiguration + @ComponentScan ;其中@ComponentScan默認掃描的是啟動類所在包以及其子包下的所有帶有@Component的類。
而在該項目中,controller類是創建在與啟動類同級的目錄下的。
解決方法:在啟動類上面添加@ComponentScan("需要掃描的注解所在包")注解
或者將需要掃描的注解放在啟動類所在包或者其子包下
3.swagger測試方法時參數無法進入controller
原因1:缺少jar包
解決方法:在pom.xml中添加jar包依賴
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</dependency>
原因2:@ApiImplicitParam注解
解決方法:刪除@ApiImplicitParam注解並
原因3:沒有指定該方法的請求類型
解決方法:加上@RequestMapping(value = "/", method = RequestMethod.POST)