博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot 集成 swagger2 小记
阅读量:7112 次
发布时间:2019-06-28

本文共 2092 字,大约阅读时间需要 6 分钟。

环境:

springboot 1.5.8.RELEASE

程序员话比较少,直接上代码来的实际。

新建swagger配置

@Configurationclass SwaggerConfig {    /**     * 为了在生产环境中不启用swagger,此处需要读取配置文件中的配置     */    @Value("${swagger.enable}")    private Boolean enable;        @Bean    public Docket myApi() {        return new Docket(DocumentationType.SWAGGER_2)                .enable(enable)                .apiInfo(apiInfo())                .select()                .apis(RequestHandlerSelectors.basePackage("com.changan.carbond.rest"))                .paths(PathSelectors.any())                .build();    }    private ApiInfo apiInfo() {        return new ApiInfoBuilder()                .title(" 此处填写 生成文档的标题 ")                .description(" 此处填写 生成文档的描述 ")                .version("1.0")                .build();    }}

定义swagger文件的处理路由

@Configuration@EnableWebMvcpublic class WebMvcConfig extends WebMvcConfigurerAdapter {    ...    @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/");    }}

开启swagger功能

...@EnableSwagger2public class Application extends SpringBootServletInitializer {    public static void main(String[] args) {        ...    }}

spring-security还有话说

如果集成了spring-security 可能还需要添加一下配置进行权限控制

@Configuration@EnableWebSecurity@EnableGlobalMethodSecurity(prePostEnabled = true)public class WebSecurityConfig extends WebSecurityConfigurerAdapter {    private static final String[] AUTH_WHITELIST = {            // -- swagger ui            "/swagger-resources/**",            "/v2/api-docs",            "/webjars/**",    };        @Override        protected void configure(HttpSecurity http) throws Exception {            http.csrf().disable();            http.antMatcher("/**")                    .authorizeRequests()                    .antMatchers(AUTH_WHITELIST).permitAll()                    .anyRequest().authenticated();        }}

转载地址:http://rcwel.baihongyu.com/

你可能感兴趣的文章
matlab绘制peano(皮亚诺)曲线和koch(科赫曲线,雪花曲线)分形曲线
查看>>
Mybatis之设计模式之迭代器模式
查看>>
小程序TAB列表切换内容动态变化,scrollview高度根据内容动态获取
查看>>
swoole_table 实现原理剖析
查看>>
你需要知道面试中的10个JavaScript概念
查看>>
Java源码阅读之HashMap - JDK1.8
查看>>
Dell服务器系统安装后无法正常进入系统
查看>>
Silverlight/Windows8/WPF/WP7/HTML5周学习导读(9月17日-9月23日)
查看>>
Tap-Ahead:让移动搜索更加便捷的解决之道
查看>>
华为vlan划分,单臂路由以及静态路由
查看>>
3.VMware vsphere 5.0新体验-安装VMware Center
查看>>
趣题: 一道面试题的解法
查看>>
Android应用程序启动过程源代码分析(5)
查看>>
查询整个数据库中某个特定值所在的表和字段的方法
查看>>
在存储过程中编写正确的事务处理代码(SQL Server 2000 & 2005)
查看>>
数据库邮件
查看>>
adstrtal.sh报超时错误 ERROR : Timed out( 100000 ): Interrupted Exception
查看>>
一个前端工程师的基本修养
查看>>
JavaScript:文本域事件处理
查看>>
一步一步教你使用AgileEAS.NET基础类库进行应用开发-基础篇-演示ORM中的查询
查看>>