swagger使用

swagger和springboot有对应的版本要求,如果不一致会导致报错

可以从官网找相应的匹配版本。 我的是 spring-boot 2.5.2 对应的 swagger版本是:2.9.2
错误信息如下:

Parameter 0 of method linkDiscoverers in org.springframework.hateoas.config.HateoasConfiguration required a single bean, but 15 were found:
    - modelBuilderPluginRegistry: defined in null
    - modelPropertyBuilderPluginRegistry: defined in null
    - typeNameProviderPluginRegistry: defined in null
    - documentationPluginRegistry: defined in null
    - apiListingBuilderPluginRegistry: defined in null
    - operationBuilderPluginRegistry: defined in null
    - parameterBuilderPluginRegistry: defined in null
    - expandedParameterBuilderPluginRegistry: defined in null
    - resourceGroupingStrategyRegistry: defined in null
    - operationModelsProviderPluginRegistry: defined in null
    - defaultsProviderPluginRegistry: defined in null
    - pathDecoratorRegistry: defined in null
    - relProviderPluginRegistry: defined by method 'relProviderPluginRegistry' in class path resource [org/springframework/hateoas/config/HateoasConfiguration.class]
    - linkDiscovererRegistry: defined in null
    - entityLinksPluginRegistry: defined by method 'entityLinksPluginRegistry' in class path resource [org/springframework/hateoas/config/WebMvcEntityLinksConfiguration.class]

引入swagger的步骤

1. 引入依赖

<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>

2. 写config


@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.bwrj.fuda"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("SpringBoot整合Swagger")
                        .description("SpringBoot整合Swagger,详细信息......")
                        .version("9.0")
                        .contact(new Contact("啊啊啊啊", "blog.csdn.net", "aaa@gmail.com"))
                        .license("The Apache License")
                        .licenseUrl("http://www.baidu.com")
                        .build());
    }

}

3. 写对应的接口



@Api(tags = "公共方法")
@Slf4j
@RestController
@RequestMapping("/sys/common")
public class CommonController {
    /**
     * 文件上传统一方法
     */
    @ApiOperation(value = "/upload", notes = "文件上传统一方法")
    @PostMapping(value = "/upload")
    public Result<?> upload(HttpServletRequest request, HttpServletResponse response) {}
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容