溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

一步一步教你SSM整合swagger

發(fā)布時(shí)間:2020-06-14 01:11:35 來(lái)源:網(wǎng)絡(luò) 閱讀:3904 作者:Adam的blog 欄目:開發(fā)技術(shù)

一:什么是swagger?

?swagger是一款非常好用的寫API文檔的框架。其他自行百度

二:ssm整合swagger?

1:在maven的pom文件中引入依賴:(注意版本,否則會(huì)導(dǎo)致tomcat不能正常啟動(dòng)

<!-- 引入swagger -->
        <!--springfox的核心jar包 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!--springfox-ui的jar包(里面包含了swagger的界面靜態(tài)文件) -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!--springfox依賴的jar包;如果你的項(xiàng)目中已經(jīng)集成了無(wú)需重復(fù) -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>

2:創(chuàng)建SwaggerConfig.java文件、即:swagger的配置文件,最好放到單獨(dú)的文件夾下

@WebAppConfiguration
@EnableSwagger2
@EnableWebMvc
@ComponentScan(basePackages="com.zgz.cn.controller")
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("xxx項(xiàng)目接口文檔")
                .description("xxx項(xiàng)目接口測(cè)試")
                .version("1.0.0")
                .termsOfServiceUrl("")
                .license("")
                .licenseUrl("")
                .build();
    }
}

3:在SpringMVC的配置文件中配置:(配置所有控制器所在的包)

<!-- 配置swagger的bean -->
    <!-- 將靜態(tài)資源交由默認(rèn)的servlet處理 -->
    <mvc:default-servlet-handler/>
    <!-- 向容器自動(dòng)注入配置 -->
    <context:annotation-config/>
    <!-- 將SwaggerConfig配置類注入 -->
    <bean class="com.zgz.cn.swagger.SwaggerConfig"/>
    <!-- 配置swagger資源不被攔截 -->
<!--    <bean id="swagger2Config" class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration"/> -->
    <mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"/>
    <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>

4:在web.xml中配置所有的請(qǐng)求都經(jīng)過DispatcherServlet處理

<servlet-mapping>
        <servlet-name>springMVC</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

5:在控制器中使用注解:(swagger常用注解)
一步一步教你SSM整合swagger
6:測(cè)試(訪問地址:項(xiàng)目名/swagger-ui.html )
一步一步教你SSM整合swagger

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI