SSM中Swagger的简单使用

本文介绍了如何在SSM项目中集成Swagger2,通过简单步骤创建RESTful风格的接口文档。首先,引入Swagger依赖;然后,在Spring MVC配置文件中设置Swagger的允许访问路径;接着,创建Swagger配置类,并确保其与Controller层处于同一层级;最后,在实体类和控制器上添加必要的注解,完成后即可通过Swagger界面浏览和测试项目接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

swagger是一个RESTFUL风格的web服务框架:主要还是写给前端看

使用步骤:

  • 导入依赖:
 <!--springfox的核心jar包 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-api</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>jackson-annotations</artifactId>
                    <groupId>com.fasterxml.jackson.core</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-aop</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-context</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
                <exclusion>
                    <artifactId>spring-beans</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <!--springfox-ui的jar包(里面包含了swagger的界面静态文件) -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
  • springmvc配置文件中配置swagger的放行地址
<!--配置下 swagger 的地址放行-->
    <mvc:resources location="classpath:/META-INF/resources/" mapping="/swagger-ui.html"/>
    <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"/>
  • 添加swagger的配置类(一定要和controller或web层在同一层,否则不会生效【spring和springmvc的优先级】)
package com.hs.controller.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
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;

@EnableSwagger2 //开启 swagger2
@EnableWebMvc//声明是 web 项目
@Configuration
public class SwaggerConfig {
    @Bean
    public Docket api(ApiInfo apiInfo) {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
//                .apis(RequestHandlerSelectors.any())
                .apis(RequestHandlerSelectors.basePackage("com.hs.controller"))
                .build()
                .apiInfo(apiInfo);
    }
    @Bean
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("时间管理项目Forest接口文档")
                .description("时间管理项目Forest接口测试")
                .version("1.0.0")
                .termsOfServiceUrl("")
                .license("Apache2.0")
                .licenseUrl("http://httpd.apache.org/docs/2.0/license.html")
                .build();
    }
}


  • 在实体类中的注解配置
@ApiModel(description = "区域数据")//类上设置
public class Area implements CheckEmptyInterface{
  @ApiModelProperty(value = "区域主键,查询时返回",hidden = true)//成员变量上设置
  private Long id;
  @ApiModelProperty(value = "区域名字",required = true,example = "东城区")
  private String areaname;
  @ApiModelProperty(value = "父区域id",example = "1")
  private Long parentid;
  @ApiModelProperty(value = "状态,0 无效,1 有效",example = "1")
  private String status;
  @ApiModelProperty(value = "父区域名字,查询时返回",example = "东城区",hidden = true)
  private String parentname;//父区域名字, 冗余数据

  • 在控制器中的注解配置
@RestController
@RequestMapping("/areas")
@Api(tags = "区域管理模块" )//在接口文档的 controller 名字前面显示
public class AreaController {
    @Autowired
    private AreaService areaService;

    @ApiOperation("添加区域")//方法上设置,在swagger的每个标签上显示
    @PutMapping("/area")
    //实体类用注解配置了,在这里就无须重新设置
    public R addArea(@RequestBody Area area) {
        areaService.addArea(area);
        return R.setOK(null);
    }

    @PostMapping("/area")
    @ApiOperation("更新区域")
    public R updateArea(@RequestBody Area area) {
        areaService.updateArea(area);
        return R.setOK(null);
    }

    @DeleteMapping("/area")
    @ApiOperation("根据 id 删除区域")
    @ApiImplicitParam(name = "ids",value = "主键",required = true)//参数注解方式1
    public R deleteAreas(@RequestParam String[] ids) {
        areaService.deleteAreas(Arrays.asList(ids));
        return R.setOK(null);
    }

    @GetMapping("/area/{id}")
    @ApiOperation("根据 id 查询区域")
    public R getAreaById(@ApiParam(name = "id",value = "主键",defaultValue = "1") @PathVariable Long id) {//参数注解方式2

        Area area = areaService.getAreaById(id);
        return R.setOK(area);
    }

    @GetMapping("/area/parent/{id}")
    @ApiOperation("根据 父id 查询子区域")
    public R getAreaByParentId(@ApiParam(name = "id",value = "父id") @PathVariable Long id) {
        List<Area> areaList = areaService.getAreaByParentId(id);
        return R.setOK(areaList);
    }
    @GetMapping("/areas")
    @ApiOperation("查询所有区域")
    public R getAllAreas(@ApiParam(name = "page",value = "第几页",defaultValue = "1") @RequestParam(defaultValue = "1") int page,@ApiParam(name = "limit",value = "每页数量",defaultValue = "10") @RequestParam(defaultValue = "10") int limit,@ApiParam(name = "exludeId",value = "被忽略的区域 id") @RequestParam String exludeId, @ApiParam(name = "areanname",value = "区域模糊名字")@RequestParam  String areanname,@ApiParam(name = "status",value = "状态",defaultValue = "-100") @RequestParam(defaultValue = "-100") int status) {
        PageInfo<Area> pageInfo = areaService.getAllAreas(page , limit, exludeId, areanname, status);
        return R.setOK(pageInfo);
    }

    @GetMapping("/areas/parent")
    @ApiOperation("查询所有父区域")
    public R getAllFirstParentAreas(@ApiParam(name = "status",value = "状态",defaultValue = "-100") @RequestParam(defaultValue = "-100") int status) {
        List<Area> areaList = areaService.getAllFirstParentAreas(status);
        return R.setOK(areaList);
    }

}

配置完成之后就可以使用swagger查看项目的接口文档了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值