温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

一步一步教你SSM整合swagger

发布时间:2020-06-14 01:11:35 来源:网络 阅读:3889 作者:Adam的blog 栏目:开发技术

一:什么是swagger?

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

二:ssm整合swagger?

1:在maven的pom文件中引入依赖:(注意版本,否则会导致tomcat不能正常启动

<!-- 引入swagger -->
        <!--springfox的核心jar包 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!--springfox-ui的jar包(里面包含了swagger的界面静态文件) -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>
        <!--springfox依赖的jar包;如果你的项目中已经集成了无需重复 -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.0</version>
        </dependency>

2:创建SwaggerConfig.java文件、即:swagger的配置文件,最好放到单独的文件夹下

@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项目接口文档")
                .description("xxx项目接口测试")
                .version("1.0.0")
                .termsOfServiceUrl("")
                .license("")
                .licenseUrl("")
                .build();
    }
}

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

<!-- 配置swagger的bean -->
    <!-- 将静态资源交由默认的servlet处理 -->
    <mvc:default-servlet-handler/>
    <!-- 向容器自动注入配置 -->
    <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中配置所有的请求都经过DispatcherServlet处理

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

5:在控制器中使用注解:(swagger常用注解)
一步一步教你SSM整合swagger
6:测试(访问地址:项目名/swagger-ui.html )
一步一步教你SSM整合swagger

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI