Spring Mvc集成Swagger2 并处理集成中的问题

1.依赖

        <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</groupId>
			<artifactId>classmate</artifactId>
			<version>1.1.0</version>
		</dependency>

注意事项:spring 的版本要跟swagger版本对应否则会出问题
此时项目spring版本为4.0.6.RELEASE

配置类

 /**
     * Swagger2配置类
     * 在与spring boot集成时,放在与Application.java同级的目录下。
     * 通过@Configuration注解,让Spring来加载该类配置。
     * 再通过@EnableSwagger2注解来启用Swagger2。
     */
    @Configuration
    @EnableWebMvc
    @EnableSwagger2
    public class Swagger2Config extends WebMvcConfigurationSupport {

        /**
         * 创建API应用
         * apiInfo() 增加API相关信息
         * 通过select()函数返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现,
         * 本例采用指定扫描的包路径来定义指定要建立API的目录。
         * @return
         */
        @Bean
        public Docket createRestApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .apiInfo(apiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.wms.pms.web.controller"))
                                    .paths(PathSelectors.any())
                                    .build();
        }

        /**
         * 创建该API的基本信息(这些基本信息会展现在文档页面中)
         * 访问地址:http://项目实际地址/swagger-ui.html
         * @return
         */
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("SwaggerAPI")
                   // .termsOfServiceUrl("127.0.0.1:8000")
                    .version("1.0")
                    .build();
        }
    }

配置文件:

<!-- swagger -->
<!--这个地方是配置类的路径-->
<bean class="com.wms.pms.common.config.Swagger2Config"></bean>
<mvc:default-servlet-handler/>
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/"/>

上面步骤集成完毕之后就可以正常使用swagger的注解了

集成过程中出现如下错误
第一个错误
org.fasterxml.classmate ResolvedParameterizedMember
原因是下面的这个依赖必须要1.1.0以上

       <dependency>
			<groupId>com.fasterxml</groupId>
			<artifactId>classmate</artifactId>
			<version>1.1.0</version>
		</dependency>

第二个错误 是一个弹窗错误
在这里插入图片描述

此时怀疑是下面这个依赖的问题之前此依赖是2.5.0以上的版本,手动修改为2.5.0以后再次访问,问题解决

下面这个依赖如果不添加会导致
http://127.0.0.1:8000/swagger-ui.html
404

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.5.0</version>
		</dependency>

遗留问题
在另外一台电脑做springmvc集成swagger2时只添加以下依赖可以正常流程进行访问swagger页面

		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.5.0</version>
		</dependency>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值