springboot集成swagger备忘

pom依赖


    io.springfox
    springfox-swagger2
    2.7.0




    io.springfox
    springfox-swagger-ui
    2.6.1

启动类同级Swagger配置类

package com.jmockit;

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;
//这两个注解,一个是swagger2的配置,一个是项目启动的时候启动swagger2.
@Configuration
@EnableSwagger2
/**
 * @author panluwen
 * @date 2018/9/4
 * @描述
 */
public class Swagger2 {
//swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //为当前包路径
                .apis(RequestHandlerSelectors.basePackage("com.jmockit.controller"))//换成自己的controller包
                .paths(PathSelectors.any())
                .build();
    }
    //构建 api文档的详细信息函数,注意这里的注解引用的是哪个
    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //页面标题
                .title("Spring Boot 测试使用 Swagger2 构建RESTful API")
                //创建人
                .contact(new Contact("panluwen", "http://www.baidu.com", ""))
                //版本号
                .version("1.0")
                //描述
                .description("API 描述")
                .build();
    }
 
 
}

controller类

@RestController
@Slf4j
@Api("UserController相关API") 
public class UserController {

    @Autowired
    private UserRepository userRepository;

    @ApiOperation(value = "插入一条person数据", notes = "对数据库插入一条person数据")
    @ApiImplicitParam(name = "person", value = "person对象", paramType = "body", required = true, dataType = "Person")
    @RequestMapping("/add")
    public String insert(@RequestBody Person person){
        log.info(person+"");
        userRepository.save(person);
        return "SUCCESS";
    }
}

具体使用参考

https://blog.csdn.net/u014231523/article/details/76522486

你可能感兴趣的:(加油,swagger,api)