• 关于使用swagger的问题


      近来在公司实现,接触到不少新的工具框架,今天见识到了一个新的工具,它的存在好像是情理之中的,但是以前就没有遇到这东西。那就是swagger,它的功能就是把你写的controller的内容都集合到一起方便测试。或者说是把接口都集合在一起。什么样的感觉?看图就明白。

      有了它,感觉方便了很多,一个是不用打开postman之类的测试工具了,另一方面连路径参数什么的都不用写了,让人兴奋。

      介绍一下怎么安装,我使用的是maven项目,maven项目在start.spring.io那里生成什么的都可以,至少加个web,然后在pom.xml添加上下面的代码:

        <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>
            <dependency>
                <groupId>joda-time</groupId>
                <artifactId>joda-time</artifactId>
                <version>2.9.9</version>
            </dependency>

      这把我遇到需要的都加上了。包括了一些需要用到的jar包什么的。

      接着写个关于它的配置文件:  

    @Configuration
    @EnableWebMvc
    @EnableSwagger2
    @ComponentScan(basePackages = {"com.example.swagger.swagger"})
    public class SwaggerConfig {
    
        ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("XXXX Web Selfservice APIs")
                    .description("")
                    .license("")
                    .licenseUrl("")
                    .termsOfServiceUrl("")
                    .version("1.0.0")
                    .build();
        }
    
        @Bean
        public Docket customImplementation() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.example.swagger.swagger"))
                    .build()
                    .directModelSubstitute(org.joda.time.LocalDate.class, java.sql.Date.class)
                    .directModelSubstitute(org.joda.time.DateTime.class, java.util.Date.class)
                    .apiInfo(apiInfo());
        }
    }

      下一步是要增加它在项目的配置文件:

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

       最后就是写一个controller了:

    @Api(value = "controller信息")
    @RestController
    @EnableAutoConfiguration
    @RequestMapping(value = "/api/index")
    public class indexController {
    
        @ApiOperation(value = "测试swagger", notes = "这是一条注意信息")
        @RequestMapping("/hello")
        public String hello() {
            return "hello";
        }
    
    }

       现在可以打开网址:http://localhost:8080/swagger-ui.html,见证它的神奇。

  • 相关阅读:
    Python之 continue继续循环
    Python之 break退出循环
    Python之 while循环
    Python之 for循环
    java中collection、map、set、list简介 (转)
    CloudSim介绍和使用
    智能指针和动态内存
    boost signal2
    boost 信号 :
    boost库 线程使用
  • 原文地址:https://www.cnblogs.com/ljy-1471914707/p/8207226.html
Copyright © 2020-2023  润新知