SSM整合Swagger,小练手

关于swagger

刚听到swagger的时候,还不知道是个啥,百度了半天,其实就是一个提供API接口展示的UI界面,swagger根据它在各个方法注解标记来在页面上展示那些方法,还能直接在页面执行这些方法,返回响应信息并在页面展示。对于后台的咱们来说,就不需要写个啥前端界面了,用来熟悉一下ssm挺好的。

前期准备工作

首先是要在pom.xml的properties标签中

 <!--选择swagger版本-->
    <springfox.version>2.7.0</springfox.version>
    <spring.framework.version>5.0.8.RELEASE</spring.framework.version>

然后是要导入与swagger和jason相关的依赖坐标

<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>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.4</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-annotations</artifactId>
      <version>2.9.4</version>
    </dependency>
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-core</artifactId>
      <version>2.9.4</version>

下载swagger的文件swagger下载

注意swagger下载版本
在这里插入图片描述
下载完之后在web-app文件夹新建Swagger文件夹,然后将下载的文件目录下dist文件夹拷贝至项目至web-app/Swagger/
在这里插入图片描述
接着打开index.jsp,修改url路径(
http://localhost:8080/项目名/v2/api-docs(那个路径中的路径取决于你的tomcat服务器配置的url路径,要是没有项目名就不加项目名,我的tomcat部署的路径就是http://localhost:8080/Swagger_demo)
如果你下的不是2.x的版本,这行配置要改为http://localhost:8080/项目名/api-docs在这里插入图片描述

然后需要进行swagger的相关配置
在包下新建一个swagger配置类,我的位置是
在这里插入图片描述

package com.sta.SwaggerConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


@Configuration
@EnableSwagger2
public class swaggerconfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())  //显示所有类
                //.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))  //只显示添加@Api注解的类
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("开放接口API")  //粗标题
                .description("HTTP对外开放接口")   //描述
                .version("1.0.0")   //api version
                .termsOfServiceUrl("http://xxx.xxx.com")
                .license("LICENSE")   //链接名称
                .licenseUrl("http://xxx.xxx.com")   //链接地址
                .build();
    }
}

然后在springmvc的配置文件(我的是叫springmvc.xml)中添加相关配置,将swaggerconfig类交由Spring的ioc控制。

 <!--将swaggerconfig对象交由spring管理-->
        <bean class="com.sta.SwaggerConfig.swaggerconfig"/>

修改index.jsp
在这里插入图片描述
配置完这些信息之后,可以尝试访问一下localhost:8080/Swaggerdemo/Swagger/dist/index.jsp
在这里插入图片描述
如果页面出现
Can’t read swagger JSON from http://localhost:8080/Test/api-docs
这个问题困扰了我很久,在网上找了很多方案,都是吐槽别人的坑爹,结果自己的也坑。。。我的解决方案是升级spring.framework和spring的版本,从4升级到 5,问题得到解决。

SSM的部署

在显示出页面之后,接下来就是ssm的部署了。这部分内容是之前学习过的,就不再赘述。
我把项目上传到了GitHub,没写多少,希望对你有用。
SSM整合Swagger传送门.
哦多说几句,其实Swagger只要掌握那几种注解方式的运用就行了,其他的就是普通的SSM的内容。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值