spring-boot 配置swagger

2017-12-03  本文已影响0人  Zhmingo

maven依赖

配置文件

importcom.google.common.base.Predicate;

importorg.springframework.boot.autoconfigure.web.BasicErrorController;

importorg.springframework.context.annotation.Bean;

importorg.springframework.context.annotation.Configuration;

importorg.springframework.web.bind.annotation.GetMapping;

importorg.springframework.web.bind.annotation.PostMapping;

importorg.springframework.web.bind.annotation.ResponseBody;

importorg.springframework.web.bind.annotation.RestController;

importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;

importorg.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

importspringfox.documentation.RequestHandler;

importspringfox.documentation.builders.ApiInfoBuilder;

importspringfox.documentation.builders.PathSelectors;

importspringfox.documentation.service.ApiInfo;

importspringfox.documentation.spi.DocumentationType;

importspringfox.documentation.spring.web.plugins.Docket;

importspringfox.documentation.swagger2.annotations.EnableSwagger2;

/**

* Created by yuanming on 2017/12/2.

*/

@Configuration

@EnableSwagger2

public class SwaggerConfig extends WebMvcConfigurerAdapter {

//配置页面的资源

@Override

public voidaddResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("swagger-ui.html")

.addResourceLocations("classpath:/META-INF/resources/");

registry.addResourceHandler("/webjars*")

.addResourceLocations("classpath:/META-INF/resources/webjars/");

}

@Bean

publicDocketcreateRestApi() {

Predicate predicate =newPredicate() {

@Override

public booleanapply(RequestHandler requestHandler) {

Class declearingClass = requestHandler.declaringClass();

if(declearingClass == BasicErrorController.class)

return false;

if(requestHandler.isAnnotatedWith(ResponseBody.class))

return true;

if(requestHandler.isAnnotatedWith(RestController.class))

return true;

if(requestHandler.isAnnotatedWith(PostMapping.class))

return true;

if(requestHandler.isAnnotatedWith(GetMapping.class))

return true;

return false;

}

};

return newDocket(DocumentationType.SWAGGER_2)

.apiInfo(apiInfo())

.select()

.apis(predicate)

.paths(PathSelectors.any())

.build();

}

privateApiInfoapiInfo() {

return newApiInfoBuilder()

.title("七招办艺考招生考试系统 v2.0")

.description("更多相关内容请联系:13798958173")

.version("2.0")

.build();

}

}

说明

继承WebMvcConfigurerAdapter只是为了让资源文件访问的时候不会404

原因自查

由于swagger包的资源文件跟spring-boot 定义的规则不一样,因而导致部分人进行配置的时候访问localhost:8080/swagger-ui.html的时候会有404的状况出现

上一篇下一篇

猜你喜欢

热点阅读