【Java】--swagger use

1. Preface to swagger

During development, the server provides an interface for the client or front-end to use. As a programmer, you always need to self-test the interface in advance. It is generally common to use PostMan to simulate. In addition, swagger can perform interface simulation self-test very well, which is convenient.

2. sprngboot integrates swagger

1. Basic configuration

Reference jar package

    <!-- swagger -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-spring-web</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>

config file configuration

@Configuration // 配置类
@EnableSwagger2 // 开启 swagger2 的自动配置
public class SwaggerConfig {
    
    
}

2. Specific cases

@Configuration // 配置类
@EnableSwagger2 // 开启 swagger2 的自动配置
public class SwaggerConfig {
    
    

//    @Bean
//    public Docket docket() {
    
    
//        // 创建一个 swagger 的 bean 实例
//        return new Docket(DocumentationType.SWAGGER_2)
//                // 配置基本信息
//                .apiInfo(apiInfo());
//    }
//
//    // 基本信息设置
//    private ApiInfo apiInfo() {
    
    
//        Contact contact = new Contact(
//                "wwy", // 作者姓名
//                "https://blog.csdn.net/xunmengyou1990?spm=1010.2135.3001.5343", // 作者网址
//                "**@qq.com"); // 作者邮箱
//        return new ApiInfoBuilder()
//                .title("swagger") // 标题
//                .description("swagger文档") // 描述
//                .termsOfServiceUrl("https://blog.csdn.net/xunmengyou1990?spm=1010.2135.3001.5343") // 跳转连接
//                .version("1.0") // 版本
//                .license("Swagger教程")
//                .licenseUrl("https://blog.csdn.net/xunmengyou1990?spm=1010.2135.3001.5343")
//                .contact(contact)
//                .build();
//    }

    @Bean
    public Docket docket1() {
    
    
        // 创建一个 swagger 的 bean 实例
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("group1")
                .select() // 设置扫描接口
                .apis(RequestHandlerSelectors   // 配置如何扫描接口
                                //.any() // 扫描全部的接口,默认
                                //.none() // 全部不扫描
                             //   .basePackage("com.**.controller") // 扫描指定包下的接口,最为常用
                        //.withClassAnnotation(**Controller.class) // 扫描带有指定注解的类下所有接口
                        .withMethodAnnotation(ApiOperation.class) // 官方自带的注解

                )
                .paths(PathSelectors
                                .any() // 满足条件的路径,该断言总为true
                        //.none() // 不满足条件的路径,该断言总为false(可用于生成环境屏蔽 swagger)
                        //.ant("/**") // 满足字符串表达式路径
                        //.regex("") // 符合正则的路径
                )
                .build();
    }

    @Bean
    public Docket docket2() {
    
    
        // 创建一个 swagger 的 bean 实例
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("group2")
                .select() // 设置扫描接口
                .apis(RequestHandlerSelectors   // 配置如何扫描接口
                        //.any() // 扫描全部的接口,默认
                        //.none() // 全部不扫描
                        //   .basePackage("com.**.controller") // 扫描指定包下的接口,最为常用
                        //.withClassAnnotation(**Controller.class) // 扫描带有指定注解的类下所有接口
                        .withMethodAnnotation(SwaggerAnnotion.class) // 自定义注解,进行扫描

                )
                .paths(PathSelectors
                                .any() // 满足条件的路径,该断言总为true
                        //.none() // 不满足条件的路径,该断言总为false(可用于生成环境屏蔽 swagger)
                        //.ant("/**") // 满足字符串表达式路径
                        //.regex("") // 符合正则的路径
                )
                .build();
    }

}

According to the above configuration, two groups are set up, and each group is scanned through custom annotations.

	@RequestMapping("/sayHello.do")
	@ResponseBody //这是返回json格式
	@ApiOperation(value="hello接口")
	public Object sayHello() {
    
    
		return "Hello,World!";
	}

	@RequestMapping("/wrong.html")
	@SwaggerAnnotion(value="错误页面")
	public Object wrongPage(){
    
    
		return "wrong";
	}

In this way, annotations can be added to certain methods to implement grouping swagger, which is more convenient!

Guess you like

Origin blog.csdn.net/xunmengyou1990/article/details/125962422