zoukankan      html  css  js  c++  java
  • Springboot集成swagger

    1.引入jar包

    		<!-- springfox-swagger -->
    	    <dependency>
    	        <groupId>io.springfox</groupId>
    	        <artifactId>springfox-boot-starter</artifactId>
    	        <version>3.0.0</version>
    	    </dependency>
    

    2.编写配置类

    package com.qyc.Microservice.config;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.env.AbstractEnvironment;
    
    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;
    
    
    /**
     * 访问地址:
     *   /swagger-ui/index.html
     */
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    	
        @Autowired
        private AbstractEnvironment env;
    	
        @Bean
        public Docket createRestApi() {
        	//是否开启swagger,正式环境一般是需要关闭的(避免不必要的漏洞暴露!),可根据springboot的多环境配置进行设置
        	String swaggerflag = env.getProperty("swagger.enable");
        	
        	if("true".equals(swaggerflag)){
                return new Docket(DocumentationType.SWAGGER_2)
                        .pathMapping("/")
                        .select()
                        .apis(RequestHandlerSelectors.basePackage("com.qyc.Microservice.controller"))
                        .paths(PathSelectors.any())
                        .build().apiInfo(new ApiInfoBuilder()
                                .title("项目接口接口说明")
                                .description("详细信息......")
                                .version("1.0")
                                .contact(new Contact("JoePotter","https://www.cnblogs.com/JoePotter/","252177507@qq.com"))
                                .license("The Apache License")
                                .licenseUrl("https://www.cnblogs.com/JoePotter/")
                                .build());
        	}else {
                return new Docket(DocumentationType.SWAGGER_2)
                        .pathMapping("/")
                        .select()
                        .apis(RequestHandlerSelectors.basePackage("com.qyc.Microservice.controller"))
                        .paths(PathSelectors.any())
                        .build().apiInfo(new ApiInfoBuilder()
                                .title("项目接口接口说明")
                                .description("详细信息......")
                                .version("1.0")
                                .contact(new Contact("JoePotter","https://www.cnblogs.com/JoePotter/","252177507@qq.com"))
                                .license("The Apache License")
                                .licenseUrl("https://www.cnblogs.com/JoePotter/")
                                .build()).enable(false);
        	}
        }
    }
    

    3.对接口和实体类添加注释,生成doc。常用的标记如下

    @Api()用于类;
    标识这个类是swagger的资源
      tags–表示分组说明标签

    @ApiOperation()用于方法;
    表示一个http请求的操作
      value用于方法描述

      notes用于提示内容

    @ApiModel()用于实体类
    表示对类进行说明,用于参数用实体类接收

    value–表示对象名
    description–描述


    @ApiModelProperty()用于实体类字段
    表示对model属性的说明或者数据操作更改
      value–字段说明
      name–重写属性名字
      dataType–重写属性类型
      required–是否必填
      example–举例说明
      hidden–隐藏


    @ApiImplicitParam() 用于 controller 方法
    表示单独的请求参数
      name–参数ming
      value–参数说明
      dataType–数据类型
      paramType–参数类型
      example–举例说明

    @ApiImplicitParams() 用于 controller 方法,包含多个 @ApiImplicitParam


    @ApiIgnore()用于类或者方法上,可以不被swagger显示在页面上

    说明:简单的标记只需要@Api(tags="") 和 @ApiOperation(value="",notes="")

    @RestController
    @RequestMapping("/user")
    @Api(tags = "用户登录")
    public class LoginController extends BaseController {
    
    	@Autowired
    	private UserService userService;
    	
    	@PostMapping("/save")
    	@ApiOperation(value = "新建用户!",notes = "提示信息。")
    	public void save(@Valid User user) {
    		userService.saveMapper( user);
    	}
    

      

    有任何需要沟通交流的联系QQ群:276483863 加好友备注【博客园技术交流】
  • 相关阅读:
    Leetcode Reverse Words in a String
    topcoder SRM 619 DIV2 GoodCompanyDivTwo
    topcoder SRM 618 DIV2 MovingRooksDiv2
    topcoder SRM 618 DIV2 WritingWords
    topcoder SRM 618 DIV2 LongWordsDiv2
    Zepto Code Rush 2014 A. Feed with Candy
    Zepto Code Rush 2014 B
    Codeforces Round #245 (Div. 2) B
    Codeforces Round #245 (Div. 2) A
    Codeforces Round #247 (Div. 2) B
  • 原文地址:https://www.cnblogs.com/JoePotter/p/14672041.html
Copyright © 2011-2022 走看看