先后端分离ssm配置swagger接口文档

以前配置过springboot,相比ssm要简单不少,如今记录一下ssm的配置html

在pom.xml中加入依赖

<!--swagger自己不支持spring mvc的,springfox把swagger包装了一下,让他能够支持springmvc-->
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger2</artifactId>
      <version>2.6.1</version>
    </dependency>
    <dependency>
      <groupId>io.springfox</groupId>
      <artifactId>springfox-swagger-ui</artifactId>
      <version>2.6.1</version>
    </dependency>

添加配置类SwaggerConfig.java

@WebAppConfiguration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages = "com.maxcore.controller")
public class SwaggerConfig {


    @Bean
    public Docket customDocket() {
        //
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("娜", "https://www.baidu.me", "baidu_666@icloud.com");
        return new ApiInfo("仿简书前台API接口",//大标题 title
                "Swagger测试demo",//小标题
                "0.0.1",//版本
                "www.baidu.com",//termsOfServiceUrl
                contact,//做者
                "Blog",//连接显示文字
                "https://www.baidu.me"//网站连接
        );
    }


}

在dispatcher-servlet.xml(springmvc的配置文件)中加入以下配置

<bean class="com.maxcore.config.SwaggerConfig" />

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

要在controller层添加注解

最后启动项目,访问swagger接口文档的路径必定要对,否则一直报404,你觉得你没配置对,实际上是你路径不对,笔者在这里表示有很痛的领悟

笔者的本地的访问路径是 http://localhost/jianShuSSM_war/swagger-ui.htmljava

通常都是 http://ip地址:端口(默认80,不显示)/项目名/swagger-ui.htmlgit

githubgithub

我的网站web

相关文章
相关标签/搜索