springmvc swagger2

1、pom文件中增加swagger饮用

  <properties>
    <swagger.version>2.9.2</swagger.version>
  </properties>


<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>${swagger.version}</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>${swagger.version}</version> </dependency>

2、spring.xml 中增加swagger配置

<!-- swagger配置类 下面会写到 -->

<bean class="com.xxxx.SwaggerConfig" />
<mvc:resources mapping="swagger-ui.html" location="classpath:/META-INF/resources/"/>
<mvc:resources mapping="/webjars/**" location="classpath:/META-INF/resources/webjars/" />

3、增加swagger配置

package com.xxx.config;

import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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;

/**
 * @author :abel.he
 * @date :Created in 2021/12/6 10:51
 * @description:swagger 配置
 * @modified By:
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig  {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
//                包路劲
//                .apis(RequestHandlerSelectors.basePackage("com.tn.controller"))
//                保安@Api的会显示,其他不会显示
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class))
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("人力资源接口")
                .description("人力资源接口-swagger")
                .version("1.0.0")
                .termsOfServiceUrl("http://192.168.20.126:8080/hrserver")
                .license("LICENSE")
                .licenseUrl("http://192.168.20.126:8080/hrserver")
                .build();
    }

}
View Code

 

访问:

http://ip:端口/项目名/swagger-ui.html

http://localhost:8080/swagger-ui.html

 

问题:访问swagger-ui.html时,404原因是:

spring.xml配置文件中,标签mvc:resources  属性location 最后没有加"/"

posted @ 2021-12-06 17:31  译林  阅读(93)  评论(0编辑  收藏  举报