SpringBoot2 集成swagger2.9.2

POM.XML

      
        
        
            io.springfox
            springfox-swagger2
            2.9.2
            
                
                
                    io.swagger
                    swagger-annotations
                
                
                    io.swagger
                    swagger-models
                
            
        
        
        
            io.swagger
            swagger-annotations
            1.5.21
        
        
            io.swagger
            swagger-models
            1.5.21
        

        
            com.github.xiaoymin
            swagger-bootstrap-ui
            1.9.4
        

配置类:

SwaggerConfig.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.basePackage("配置类的全路径"))
            .paths(PathSelectors.any())
            .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
            .title("Spring Boot中使用Swagger2构建RESTful API")
            .description("rest api 文档构建利器")

            .contact(new Contact("Tencent", "http://www.baidu.com", ""))
            .version("1.0")
            .build();
    }

}

mdel:


import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.hibernate.annotations.GenericGenerator;
import org.springframework.format.annotation.DateTimeFormat;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Date;

@Data
@Entity
@GenericGenerator(name = "uuid", strategy = "uuid")
@Table(name = "test")
public class Test implements Serializable {
    @Id
    @GeneratedValue(generator = "uuid")
    @Column(length = 32)
    @ApiModelProperty(value = "id")
    private String id;

    @ApiModelProperty(value = "参数说明",required = true)
    private String name;

    @ApiModelProperty( example = "2019-09-04 12:11:11", dataType = "Date", value = "参数说明")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

}

TestController.java

@Api(value = "API test")
public class TestControlller {

    @PostMapping(value = "register")
    @ApiOperation(value = "测试")
    public ResponseT register(@ApiParam(name = "taskInfoStr", value = "测试实体") @RequestBody String taskInfoStr) {
        logger.info(taskInfoStr);
        TaskInfo taskInfo = JSON.parseObject(taskInfoStr, TaskInfo.class);
        taskInfo.setId(UUID.randomUUID().toString());
        logger.info(taskInfo.toString());
        taskInfoService.save(taskInfo);
       

        return ResponseT.status(200).msg("测试");
    }
}

启动项目后打开 http://localhost:port/doc.html

SpringBoot2 集成swagger2.9.2_第1张图片

你可能感兴趣的:(springboot)