Spring Boot 2.x使用swagger2.8.0生成在线API文档

1,在pom文件里面添加swagger的依赖和ui依赖



    4.0.0

    com.itpengwei.ideaboot
    swagger-demo
    0.0.1-SNAPSHOT
    jar

    swagger-demo
    Demo project for Spring Boot

    
        org.springframework.boot
        spring-boot-starter-parent
        2.0.3.RELEASE
         
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        
        
            io.springfox
            springfox-swagger2
            2.8.0
        
        
            io.springfox
            springfox-swagger-ui
            2.8.0
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    



2,创建配置类

package com.itpengwei.config;

import io.swagger.annotations.ApiOperation;
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 SwaggerConfig {
//    @Bean
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2).select().
                apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).build();
    }

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                // 自行修改为自己的包路径
                .apis(RequestHandlerSelectors.basePackage("com.itpengwei.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("api文档")
                .description("restFUL 风格接口")
                .version("1.0")
                .build();
    }
}

3,创建Controller

package com.itpengwei.controller;

import com.itpengwei.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@Api(value = "/user", tags = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController {
    @ApiOperation(value = "展示用户信息",notes = "展示用户")
    @GetMapping("/userList")
    public List getUserlist() {
        List users = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            User user = new User();
            user.setSex("男");
            user.setAge(i);
            user.setUsername("test" + i);
            users.add(user);
        }
        return users;
    }
    @ApiOperation(value = "用户添加",notes = "添加用户")
    @ApiImplicitParam(name = "user",value = "User",required = true,dataType ="User")
    @PostMapping(value = "/addUser")
    public String addUser(User user){
        return "添加成功";
    }
    @ApiOperation(value = "删除用户",notes = "删除用户")
    @ApiImplicitParam(name = "id",value = "id",required = true,dataType = "Integer")
    @DeleteMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id")Integer id){
        return "删除成功";
    }
    @ApiOperation(value = "修改用户",notes = "修改用户")
    @PutMapping("/updateUser")
    @ApiImplicitParam(name = "user",value = "User",required = true,dataType = "User")
    public String updateUser(User user){
        return "修改成功";
    }
}

4,创建启动类

package com.itpengwei;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class SwaggerDemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(SwaggerDemoApplication.class, args);
    }
}

5,启动,效果图如下

Spring Boot 2.x使用swagger2.8.0生成在线API文档_第1张图片

你可能感兴趣的:(springboot,swagger)