SpringBoot integrates Swagger to implement online interface documentation

SpringBoot integrates Swagger to implement online interface documentation

  1. Create a SpringBoot project, build with maven.
  2. Add the following dependencies to the pom file
	<!--swagger-->
	<dependency>
	    <groupId>io.springfox</groupId>
	    <artifactId>springfox-swagger2</artifactId>
	    <version>2.4.0</version>
	</dependency>
	<dependency>
	    <groupId>io.springfox</groupId>
	    <artifactId>springfox-swagger-ui</artifactId>
	    <version>2.4.0</version>
	</dependency>
  1. Write a configuration class
@Configuration
@EnableSwagger2
public class Swagger2 {
    
    

    @Bean
    public Docket createRestApi(){
    
    
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select()
                .apis(RequestHandlerSelectors.basePackage("com.naihe.swagger_test.controller."))
                .paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfo(){
    
    
        return new ApiInfoBuilder()
                .title("swagger集成测试")
                .contact(new Contact("naihe", "http://www.itnaihe.club:8081", "[email protected]"))
                .description("欢迎访问接口文档,这是描述信息")
                .version("1.0").build();
    }
}
  1. Used in Controller
@RestController
@RequestMapping("/details")
@Api(value = "详情相关业务接口", tags = "详情业务相关的controller")
public class DetailsController {
    
    
    @GetMapping("/getDetails")
    @ApiOperation(value = "xxx", name = "xxx")
    public String getDetails(){
    
    
        return "details";
    }
}

Common annotations and their function descriptions

Please add image description

Guess you like

Origin blog.csdn.net/qq_41570752/article/details/123001696