前后端分離ssm配置swagger接口文檔


之前配置過springboot,相比ssm要簡單很多,現在記錄一下ssm的配置

在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.html

一般都是
http://ip地址:端口(默認80,不顯示)/項目名/swagger-ui.html


github

個人網站


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM