springMvc集成swagger问题记录

本文记录了springMvc集成swagger的过程及遇到的问题。在集成中,涉及swagger的maven依赖和配置,通过@Api和@ApiOperation注解为接口添加信息。问题包括:错误创建bean 'documentationPluginsBootstrapper',解决办法是在SwaggerConfig类上添加@EnableWebMvc注解;接口信息未显示,解决方法是使用@ComponentScan注解扫描controller包。

springMvc集成swagger问题记录

1. springMvc集成swagger

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

(1)swagger的maven依赖

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.2</version>
        </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类接口都可以被扫描到。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值