Swagger-ui的使用和遇到的问题的处理

导Jar包:

<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger2</artifactId>
   <version>2.7.0</version>
</dependency>
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-swagger-ui</artifactId>
   <version>2.7.0</version>
</dependency>

Swagger-ui的配置类:

@Configuration
@EnableSwagger2
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {

    //这个方法不用动
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");

        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

    /**
     * swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
     * @return
     */
    @Bean
    public Docket createRestfulApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("top.dolo.controller")) //暴露接口地址的包路径
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 构建 api文档的详细信息函数,注意这里的注解引用的是哪个
     * @return
     */
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                //页面标题
                .title("测试的RESTful API")
                //版本号
                .description("访问接口API")
                .termsOfServiceUrl("http://dologzs.top/")
                .version("1.0")
                .build();
    }

}

Controller的简单配置(主要是看颜色标注的地方):

@Api(value = "TestController", tags = "TestApi", description = "Testcontroller")
@CrossOrigin
@RestController
@RequestMapping("/test")
public class TestController {

    @Resource
    private TestService service;

 
    @ApiOperation(value="Test", notes="Test")
    @ApiImplicitParam(name = "Test", value = "Test", required = true, dataType = "TestEntity")
    @PostMapping("/test")
    public String annotateWAS(@RequestBody Test test) throws Exception {
        return "Hello  World!";
    }

}

关于遇到的问题:

以上配置基本配好了之后,如果出现访问页面空白或者不显示,请清除缓存,清除缓存,清除缓存,清除缓存,清除缓存

就是浏览器缓存的原因,你清一下浏览器的缓存就好了,就这么简单!

就是浏览器缓存的原因,你清一下浏览器的缓存就好了,就这么简单!

就是浏览器缓存的原因,你清一下浏览器的缓存就好了,就这么简单!

除非你配置不正确!

猜你喜欢

转载自my.oschina.net/u/3852748/blog/2960947