Spring boot 集合 swagger2

1、 pom.xml 导入 


        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.9.2version>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.9.2version>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

 

2、 创建 swagger2 配置类

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.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)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.lzb.mydemo"))
                .paths(PathSelectors.any())

                .build().apiInfo(new ApiInfoBuilder()
                        .title("管理系统-API文档")
                        .description("API文档,详细信息......")
                        .version("1.0")
                        .contact(new Contact("联系开发人员", "blog.csdn.net", "[email protected]"))
                        .license("The Apache License") // 执照
                        .licenseUrl("http://www.baidu.com") //许可证网址
                        .build()
//                .securitySchemes(securitySchemes()
                );
    }

 

启动项目, 网页输入:http://localhost:8080/swagger-ui.html,如下页面,说明已经配置成功:Spring boot 集合 swagger2_第1张图片

 

 

3、创建 controller

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "测试相关接口")
@RequestMapping("/test")
public class TestController {


    @RequestMapping(value = "/facade/front/address/add", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ApiOperation(value = "增加用户地址")
    public String addAddress(
            @ApiParam(required = true, value = "参数输入")
            @RequestParam(value = "enter", defaultValue = "默认值(前面的value一定要写,不然这恶搞默认值就没用", required = true) String enter
    ) {
        return enter ;
    }

Spring boot 集合 swagger2_第2张图片

 

   集合成功!

 

你可能感兴趣的:(Spring boot 集合 swagger2)