先後端分離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

相關文章
相關標籤/搜索