SSM集成 Swagger

本文详细介绍了如何在SSM项目中集成Swagger,包括引入Maven依赖,创建Swagger配置类,以及在Controller层使用各种注解如@Api、@ApiOperation等进行接口文档的详细说明。

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

SSM集成 Swagger

1. 引入Swagger的maven坐标

      <!-- swagger -->
        <dependency>                       
            <groupId>com.mangofactory</groupId>                       
            <artifactId>swagger-springmvc</artifactId>                       
            <version>1.0.2</version>                   
        </dependency>               
        <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.7.5</version>                   
        </dependency>

2. 新建Swagger.java

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2 {
    @Bean
    public Docket userApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()// 选择那些路径和api会生成document
                .apis(RequestHandlerSelectors.any()) // 对所有api进行监控
                .paths(PathSelectors.any()) // 对所有路径进行监�?
                .build();
    }
    @Bean
    public RequestMappingInfoHandlerMapping requestMapping() {
        return new RequestMappingHandlerMapping();
    }
}

3. 在controller层进行配置

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/TestExcelFast")
@Api(value = "restful", description = "测试")
public class Test {
    @ApiOperation(value = "测试专用")
    @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ResponseBody
    public String test() {
        String str = "123";
        return str;
    }
}

4. 注解及说明

@Api

用在请求类上 , 表示对类进行说明

  • tags=“说明该类的作用,可以在UI界面上看大的注解”
  • value=“该参数没说明意义,在UI界面上也看到,所以不需要配置”

@ApiOperation

用在请求方法上 , 说明方法的用途和作用

  • value=“说明方法的用途和作用”
  • notes=“方法的备注说明”

@ApiImplicitParams

用在请求方法上 , 表示一组参数说明

  • @ApiImplicitParam: 用在@ApiImplicitParams 注解中 , 指定一个请求参数的各个方面
    • name : 参数名
    • value : 参数的汉字说明 , 解释
    • required : 参数是否必传
    • paramType : 参数放在哪里
      • header --> 请求参数的获取  @RequestHeader
      • query --> 请求参数的获取 @RequestParam
      • path --> 用于restful接口 请求参数获取 @PathVariable
      • body 不常用
      • form 不常用
    • dataType : 参数类型 , 默认String , 其它值 dataType=“Integer”
    • defaultValue : 参数默认值

@ApiResponses

用在请求方法上 , 表示一组响应

  • @ApiResponse : 用在@ApiResponse中 , 一般用于表达一个错误的响应信息
    • code : 数字 , 例如4040
    • message : 例如 “请求参数填写错误”
    • response : 抛出异常的类

@ApiModel

用于响应类上 , 表示一个返回响应数据的信息(这种一般在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)

  • @ApiModelProperty : 用在属性上 , 描述响应类的属性

参考

https://blog.youkuaiyun.com/qq_34263321/article/details/80035534

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值