美文网首页
swagger2的使用

swagger2的使用

作者: 英文名叫夏天 | 来源:发表于2018-12-14 15:27 被阅读0次

    引入包

    <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <swagger.version>2.6.1</swagger.version>
        </properties>
      <!-- swagger -->
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>${swagger.version}</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>${swagger.version}</version>
            </dependency>
    
    

    在application的main函数中设置

    import java.text.SimpleDateFormat;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.domain.EntityScan;
    import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
    import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
    import org.springframework.boot.web.client.RestTemplateBuilder;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    import org.springframework.web.client.RestTemplate;
    import org.springframework.web.servlet.ModelAndView;
    
    import com.fasterxml.jackson.databind.DeserializationFeature;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import io.swagger.annotations.ApiOperation;
    import springfox.documentation.annotations.ApiIgnore;
    import springfox.documentation.builders.ApiInfoBuilder;
    import springfox.documentation.builders.PathSelectors;
    import springfox.documentation.builders.RequestHandlerSelectors;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    /**.
     * @date: 2018/12/3
     */
    @Configuration
    @ComponentScan("com.healthlinkiot.autobottest")
    @EntityScan("com.healthlinkiot.autobottest")
    @RestController
    @EnableSwagger2
    @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
    public class AutobotTestBackendApplication {
        public static void main(String[] args) throws Exception {
            SpringApplication.run(AutobotTestBackendApplication.class, args);
        }
    
        @RequestMapping(value = "/", method = RequestMethod.GET)
        @ApiIgnore()
        @ApiOperation(value = "重定向到api首页")
        public ModelAndView index() {
            return new ModelAndView("redirect:/swagger-ui.html");
        }
    
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.healthlinkiot.autobottest"))
                .paths(PathSelectors.any())
                .build();
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                .title("autobot-test")
                .description("autobot-test")
                .version("1.0")
                .build();
        }
    }
    
    • 使用方法

    相关文章

      网友评论

          本文标题:swagger2的使用

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