swagger作用

本文介绍了如何在SpringBoot项目中使用Swagger2生成接口文档,包括添加依赖、配置类指定包、在控制器类中添加@Api和@ApiOperation注解。通过这些注解,可以自定义API文档的标题、描述和功能说明。

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

根据在代码中使用自定义的注解来生成接口文档

使用方法:

    1) 添加jar包

 <!--SpringBoot整合Swagger2生成接口文档-->
<dependency>
    <groupId>com.spring4all</groupId>
    <artifactId>swagger-spring-boot-starter</artifactId>
    <version>1.7.0.RELEASE</version>
</dependency>

2)添加配置类,指定相关包com.micro.controller

package com.micro.config;

import com.spring4all.swagger.SwaggerProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
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 Swagger2Config {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.micro.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("商品服务接口文档")
                        .description("商品服务接口文档,详细信息......")
                        .version("1.0")
                        .contact("23423423423@qq.com")
                        .license("The Apache License")
                        .licenseUrl("http://www.baidu.com")
                        .build());
    }
}

3)用的类中添加注解

package com.micro.controller;

import com.micro.mapper.ProductMapper;
import com.micro.pojo.Product;
import com.micro.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/product")
@Api(value="商品controller",tags={"商品服务接口"})
public class ProductController {

    @Autowired
    ProductService productService;

    /**
     * 01-对外暴露的接口
     * @return
     */
    @ApiOperation(value="查询所有的商品",notes = "测试")
    @GetMapping("/findProducts")
    public List<Product> findProducts(){
        return productService.findProducts();
    }


}

4.访问:http://127.0.0.1:8081/swagger-ui.html

备注:

@Api:用在请求的类上,表示对类的说明

tags="说明该类的作用,可以在UI界面上看到的注解"

value="该参数没什么意义,在UI界面上也看到,所以不需要配置"

@ApiOperation:用在请求的方法上,说明方法的用途、作用

value="说明方法的用途、作用"

notes="方法的备注说明"

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值