springMvc集成swagger问题记录

springMvc集成swagger问题记录

1. springMvc集成swagger

springMvc集成swagger有几种方式,这里选取其中的一种。(默认springMvc项目搭建完毕,添加swagger)

(1)swagger的maven依赖

        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>2.5.0version>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>2.5.0version>
        dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.coregroupId>
            <artifactId>jackson-databindartifactId>
            <version>2.6.2version>
        dependency>

(2)swagger配置

package com.test.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@EnableSwagger2
@EnableWebMvc
@Configuration
@ComponentScan("com.test.controller")
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.test.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springMvc集成swagger测试")
                .description("just for test")
                .version("1.1")
                .build();
    }
}

可以使用swagger的注解,比如@Api,@ApiOperation等为controller方法添加信息,不添加的话可以直接启动。正常情况下,登录 http://ip:port/项目名/swagger-ui.html 就可以看到了。

2. 遇到的问题

(1)Error creating bean with name ‘documentationPluginsBootstrapper’ defined in URL [jar:file:/D:/workspace/demo/target/demo/WEB-INF/lib/springfox-spring-web-2.5.0

springMvc集成时注意在SwaggerConfig类上添加@EnableWebMvc注解,即可解决。springboot

则不需要。

(2)可以打开swagger页面,但是没有接口信息,只有配置类里面的title、description等信息。

直接在SwaggerConfig类上添加注解 @ComponentScan(“com.test.controller”),扫描的包为controller包,这个包下面所有的controller类接口都可以被扫描到。

你可能感兴趣的:(常用工具)