【Spring Boot】【三】集成swagger ui

1、添加依赖

        
            io.springfox
            springfox-swagger2
            2.9.2
        
        
            io.springfox
            springfox-swagger-ui
            2.9.2
        

2、启动swagger ui注解

package com.example.hello;

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
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("测试Swagger工程")
                .description("测试学习一下")
                .contact(
                        new Contact("陈振林", "note.abeffect.com", "[email protected]")
                )
                .version("1.0.0-SNAPSHOT")
                .build();
    }
}

3、在controller上添加注解,自动生成API

package com.example.hello;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@RestController
@Api(value = "问好管理")
public class HelloWorld {

    @GetMapping("/hello")
    @ApiOperation(value = "向指定的人问好")
    public String Hello(String name) {
        return "Hello " + name;
    }
}

4、swagger ui 链接: http://localhost:8080/swagger-ui.html (默认)

【Spring Boot】【三】集成swagger ui_第1张图片
image.png

你可能感兴趣的:(【Spring Boot】【三】集成swagger ui)