美文网首页
2019最新版springboot2.1.1-05-Swagge

2019最新版springboot2.1.1-05-Swagge

作者: 稀客大大 | 来源:发表于2019-01-03 22:49 被阅读0次

    SwaggerAPI框架

    为了方便上面的API接口调试,我们可以使用:

    • Postman:模拟POST请求
    • Swagger:描述和测试API接口

    1.添加依赖

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

    2.Swagger配置

    yml配置文件

    swagger:
      title: SpringBoot学习
      description: SpringBoot快速入门
      version: 1.0.0
      name: 稀客大大
      url: http://accp.site
      email: 877058128@qq.com
    

    Swagger配置文件

    /**
     * @author: zed[QQ:877058128]
     * @create: 2018-12-26
     * @description:
     */
    @ConfigurationProperties(prefix = "swagger")
    @Configuration //必须存在
    @EnableSwagger2 //必须存在
    @EnableWebMvc //必须存在
    @Data  //lomlok
    //必须存在 扫描的API Controller包
    @ComponentScan(basePackages = {"com.zed.demo.controller"})
    public class SwaggerConfig{
    
        private String title;
        private String description;
        private String version;
    
        private String name;
        private String url;
        private String email;
    
        @Bean
        public Docket customDocket() {
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo());
        }
    
        private ApiInfo apiInfo() {
            Contact contact = new Contact(name, url, email);
            return new ApiInfoBuilder()
                    .title(title)
                    .description(description)
                    .contact(contact)
                    .version(version)
                    .build();
        }
    }
    

    MVC配置

    作用:过滤网页静态资源

    @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/");
        }
    
    }
    

    3.使用注解

    在控制器API上添加接口描述的注解

    @RestController
    @Api(value = "用户模块")
    public class HelloController {
        @GetMapping("/hello")
        @ApiOperation(value = "测试Swagger")
        public String hello(){
            return "hello SpringBoot-swagger";
        }
    }
    
    Swagger的具体用法,请参照 B站Swagger视频

    4.Webjars的使用

    官网:https://www.webjars.org/

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>jquery</artifactId>
        <version>3.3.1-1</version>
    </dependency>
    

    通过jar包的方式引入了jQuery

    再浏览器中可以通过如下地址访问到

    http://localhost:8080/webjars/jquery/3.3.1-1/jquery.js

    地址说明:/webjars/jquery/3.3.1-1/jquery.js

    对应上面的 webjars/ artifactId/version/文件名

    可以到项目中的jar包中具体查看。

    配套视频地址 https://www.bilibili.com/video/av39775932/

    相关文章

      网友评论

          本文标题:2019最新版springboot2.1.1-05-Swagge

          本文链接:https://www.haomeiwen.com/subject/aolcrqtx.html