首先是訪問http://ip:/doc.htmlhttp://${host}:${port}/doc.htmlhttp://ip:/doc.html報錯
1、假如是SpringSecurity項目,可能是configure(WebSecurity web)沒有放行,代碼如下
@Configuration
@Lazy
public class SecurityConfig extends WebSecurityConfigurerAdapter{
@Override
public void configure(WebSecurity web) throws Exception {
web.ignoring().antMatchers(
"/login",
"/logout",
"/css/**",
"/js/**",
"/index.html",
"favicon.ico",
"/doc.html",
"/webjars/**",
"/swagger-resources/**",
"/v2/api-docs/**"
);
}
}
在啟動類中修改成如下代碼
@SpringBootApplication
@MapperScan("mapper包名")
@EnableWebMvc
public class YebApplication implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/doc.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").
addResourceLocations("classpath:/META-INF/resources/webjars/");
}
public static void main(String[] args) {
SpringApplication.run(YebApplication.class, args);
}
}
Swagger配置類
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket createRestApi(){
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
// 初始化并返回一個API選擇構(gòu)造器
.select()
// apis:添加路徑選擇條件,根據(jù)包名掃描controller類
//.apis(RequestHandlerSelectors.basePackage("要掃描包名")) // 如果不行使用下面的
// 根據(jù)注解掃描,只掃描標(biāo)有@RestController的類
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
// 設(shè)置路徑篩選,滿足條件的路徑,該斷言總為true
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("標(biāo)題")
.description("描述檔")
.contact(new Contact("作者名", "url", "郵箱"))
.version("1.0") //版本
.build();
}
}
Controller類文章來源:http://www.zghlxwxcb.cn/news/detail-540711.html
@Api(value = "/hello", tags = "測試hello")
@RestController
public class HelloController {
@ApiOperation("hello")
@GetMapping("/hello")
public String hello(){
return "hello";
}
}
?Swagger依賴文章來源地址http://www.zghlxwxcb.cn/news/detail-540711.html
<!-- swagger2 依賴-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<!-- swagger2 第三方ui依賴-->
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>swagger-bootstrap-ui</artifactId>
<version>1.9.6</version>
</dependency>
到了這里,關(guān)于swagger-bootstrap-ui 報錯No mapping for GET /doc.htm,404l,以及無法顯示接口文檔的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!