Spring Boot 配置 Swagger2

Spring Boot 配置 Swagger2

pom.xml

<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>

SwaggerConfig.java

@Configuration
@EnableSwagger2
public class Swagger2Config {
    @Bean
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .paths(PathSelectors.any())
                .build();
    }
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Swagger")
                .description("Rest API接口")
                .version("0.1")
                .build();
    }
}

HelloController.java

@RestController
@RequestMapping("hello")
@Api(tags = "Hello接口")
public class HelloController {
    @ApiOperation("首页")
    @GetMapping("")
    public List<String> index(){
        List<String> list=new ArrayList<>();
        list.add("米开朗基罗");
        list.add("拉斐尔");
        list.add("达芬奇");
        return list;
    }
    @ApiOperation("日期")
    @GetMapping("date")
    public Date date(){
        return new Date();
    }
}

注意

使用fastjson时不要排除掉spring-boot-starter-json

posted @ 2020-04-18 11:49  彦桢  阅读(424)  评论(0编辑  收藏  举报