Swagger分组和接口注释

1. swagger分组(只需要创建几个Docket类型的方法即可)

package com.Google.config;


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
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;

import java.util.ArrayList;

import static springfox.documentation.spi.DocumentationType.SWAGGER_2;

@Configuration  //将这个类交给springBoot托管
@EnableSwagger2 //启用Swagger2
@EnableWebMvc
public class SwaggerConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        WebMvcConfigurer.super.addResourceHandlers(registry);

    }
    //配置多组信息(一个Docket为一个组)
    @Bean
    public Docket docket1(){
        return new Docket(SWAGGER_2).groupName("A");
    }

    @Bean
    public Docket docket2(){
        return new Docket(SWAGGER_2).groupName("B");
    }

    @Bean
    public Docket docket3(){
        return new Docket(SWAGGER_2).groupName("C");
    }






    //配置Swagger信息
    @Bean
    public Docket docket(Environment environment){

        //设置要显示的swagger环境
        Profiles profils = Profiles.of("dev");
        //通过 environment.acceptsProfiles判断自己是否处于自己设定的环境
        boolean flag = environment.acceptsProfiles(profils);
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
                .groupName("小落")
                .enable(flag)//关闭swagger 这里我们可以根据生产环境,选择开启或者关闭swagger
                .select()
                //RequestHandlerSelectors:配置要扫描的包
                //(一般就扫描basePackage)basePackage:指定要扫描的包
                //any():全扫描
                //none():全部扫描
                //withMethodAnnotation():扫描方法上的注解 参数是注解的反射
                .apis(RequestHandlerSelectors.basePackage("com.Google.controller"))
                //过滤的路径(不扫描)这里的路径是指包的位置,而不是url
                //ant:过滤指定的路径
                //any():过滤所有路径
                //none():不过滤
                //regex():正则表达
                .paths(PathSelectors.ant("/Google/*"))
                .build();

    }


    private ApiInfo apiInfo(){
        Contact contact = new Contact("小落", "", "2034281742@qq.com"); //作者信息(Contact 联络)
        return  new ApiInfo("小落的API", //文档的标头
                "这个API用来测试", //文档的描述
                "1.0",  //文档的版本号
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList()
        );
    }

}

注意需要@bean,交给springBoot接管

2.swagger注释

用于实体类
@ApiModel("用户实体类")
用于实体类参数
@ApiModelProperty("用户名")

package com.Google.pojo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel("用户实体类")
public class user {
    @ApiModelProperty("用户名")
    String username;
    @ApiModelProperty("密码")
    String password;
}

用于接口方法
@ApiOperation("userpost控制") //只放在接口上有用
用于接口参数
@ApiParam("用户名")

package com.Google.controller;

import com.Google.pojo.user;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class helloSwagger {
    @GetMapping("/hello")
    public String hello(){
        return "你好,Swagger";
    }

    //只要我们的接口中,返回值是实体类,它就会被Swagger扫描
    @PostMapping("/postuser")
    @ApiOperation("userpost控制")    //只放在接口上有用
    public user postuser(@ApiParam("用户实体类参数") user user){
        return user;
    }

    @GetMapping("/hello1")
    public String hello1(@ApiParam("用户名") String username){
        return "你好,Swagger"+username;
    }
}

小结;

  1. 在项目上线时Swagger要注意关闭,节省运行内存
posted @ 2022-03-21 09:36  小罗要有出息  阅读(1622)  评论(0编辑  收藏  举报