SpringBoot集成swagger ui 2.9.2

1、pom依赖

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

   
        UTF-8
        UTF-8
        1.8
        2.9.2
        2.9.2
    

    
        
        
            org.projectlombok
            lombok
            true
        

        
        
            io.springfox
            springfox-swagger2
            ${springfox-swagger2.version}
        
        
            io.springfox
            springfox-swagger-ui
            ${springfox-swagger-ui.version}
        
    

2、swagger配置

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;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/05/22 09:29
 */
@Configuration
@EnableSwagger2
public class Swagger2Config {
  /**
   * swagger2的配置文件,这里可以配置swagger2的一些基本的内容,比如扫描的包等等
   *
   * @return Docket
   */
  @Bean
  public Docket createRestApi() {
    return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
        // 为扫描包路径
        .apis(RequestHandlerSelectors.basePackage("com.gitee.cashzhang27.test.swagger.controller")).paths(
            PathSelectors.any())
        .build();
  }

  /**
   * api文档的详细信息函数,注意这里的注解引用的是哪个
   *
   * @return
   */
  private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
        // //大标题
        .title("财务系统 RESTful API")
        // 版本号
        .version("1.0")
//                .termsOfServiceUrl("NO terms of service")
        // 描述
        .description("API 描述")
        //作者
        .contact(new Contact("cashZhang", "https://blog.csdn.net/m0_37726449", "[email protected]"))
//                .license("The Apache License, Version 2.0")
//                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
        .build();
  }
}

3、swagger使用

3.1 Controller

import com.gitee.cashzhang27.test.swagger.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/05/22 09:30
 */
@RestController
@RequestMapping("user")
@Api(value = "UserManagerController", description = "用户管理")
public class UserManagerController {

  @PostMapping
  @ApiOperation(value = "添加用户")
  public void addUser(User user) {
    System.out.println(user.toString());
  }

}

3.2 pojo

import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author Cash Zhang
 * @version v1.0
 * @since 2019/05/22 09:33
 */
@Data
public class User {
  /**
   * ApiModelProperty()用于方法,字段 表示对model属性的说明或者数据操作更改
   * value–字段说明
   * name–重写属性名字
   * dataType–重写属性类型
   * required–是否必填
   * example–举例说明
   * hidden–隐藏
   */
  @ApiModelProperty(value="ID",name="id",example="1")
  private Long id;

  @ApiModelProperty(value="用户名",name="username",example="zhangxin")
  private String username;

  @ApiModelProperty(value="密码",name="password",example="123456")
  private String password;
}

4、进入接口页

http://127.0.0.1:8080/swagger-ui.html

5、源码

https://gitee.com/cashzhang27/test-java/tree/master/test-boot/test-boot-seagger

你可能感兴趣的:(SpringBoot)