栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot 配置swagger2.0

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot 配置swagger2.0

1.创建项目 1.1配置application.yml
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/salay?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
    username: root
    password: root
    driver-class-name: com.mysql.cj.jdbc.Driver
​
server:
  port: 8181

这里我配置了数据库是因为一开始我创建项目的时候选择了MYSQL 驱动,所以就一起配置了,接着配置端口号8181

2.去 Maven Repository 找到jar包

 

 

 

2.配置依赖项 

    io.springfox
    springfox-swagger2
    2.9.2

    io.springfox
    springfox-swagger-ui
    2.9.2
3.将swagger依赖项存进pom.xml 文件

 

4.配置SwaggerConfig
@Configuration //必须存在
@EnableSwagger2 //必须存在
@EnableWebMvc //必须存在
//必须存在 扫描的API controller包
@ComponentScan(basePackages = {"com.milktea.huluwa.controller"})
public class SwaggerConfig {
    @Bean
    public Docket doctet(){
        return new Docket(documentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.milktea.huluwa.controller"))
                .build();
    }
​
    private ApiInfo apiInfo() {
        Contact contact = new Contact("huluwa", "http://8.130.172.207", "551160839@qq.com");
        return new ApiInfoBuilder()
                .title("葫芦娃项目API接口")
                .description("API接口")
                .contact(contact)
                .version("1.1.0")
                .build();
    }
}
5.配置WebMvcConfig
这个是为了解决跨域问题的
@Configuration
class WebMvcConfig implements WebMvcConfigurer {
​
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/meta-INF/resources/");
​
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/meta-INF/resources/webjars/");
    }
​
}
6.创建一个Hello word项目

 

7.启动运行项目

浏览http://localhost:8181/swagger-ui.html

结果

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/424522.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号