Springboot中使用swaggerUI

1.导入Maven依赖

<!--Swagger-UI API文档生产工具-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>

2.创建swaggerUI

package com.example.demo.config;

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.service.ApiInfo;
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)
                .apiInfo(apiInfo())
                .select()
                //为指定包下controller生成API文档
                .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))

                //为有 @Api注解的Controller生成API文档
                //.apis(RequestHandlerSelectors.withClassAnnotation(Api.class))

                //为有@ApiOperation注解的方法生成API文档
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中演示swaggerUI")
                .description("更多请关注https://blog.csdn.net/qq_45808264")
                .contact("thsxyym")
                .version("1.0")
                .build();
    }
}


3.结构图

Springboot中使用swaggerUI_第1张图片
controller

package com.example.demo.controller;

import com.example.demo.pojo.PmsBrand;
import com.example.demo.service.PmsBrandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@Api(tags = "PmsBrandController", description = "商品品牌管理")
@Controller
public class PmsBrandController {

    @Autowired
    private PmsBrandService pmsBrandService;

    @ApiOperation(value = "获取所有品牌,不分页")
    @RequestMapping(value = "/sel",method = RequestMethod.GET)
    @ResponseBody
    public List<PmsBrand> sel(){
        List<PmsBrand> infos=pmsBrandService.findAll();
        return infos;
    }

    @ApiOperation(value = "新增品牌")
    @ApiImplicitParam(paramType = "body")
    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    public Boolean save(@RequestBody PmsBrand pmsBrand){
        int  count=pmsBrandService.saveBrand(pmsBrand);
        return count>0?true:false;
    }

    @ApiOperation(value = "更新品牌")
    @ApiImplicitParam(paramType = "body")
    @RequestMapping(value = "/upd",method = RequestMethod.PUT)
    @ResponseBody
    public Boolean upd(@RequestBody PmsBrand pmsBrand){
        int  count=pmsBrandService.updBrand(pmsBrand);
        return count>0?true:false;
    }

    @ApiOperation(value = "按id删除")
    @ApiImplicitParam(paramType = "path")
    @RequestMapping(value = "/del/{id}",method = RequestMethod.DELETE)
    @ResponseBody
    public Boolean del(@PathVariable("id") int id){
        int  count=pmsBrandService.delBrand(id);
        return count>0?true:false;
    }
}

@Api:用在类上,说明该类的作用。
@ApiOperation:注解来给API增加方法说明。
@ApiImplicitParams : 用在方法上包含一组参数说明。
@ApiImplicitParam:用来注解来给方法入参增加说明。
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数错误"
response:抛出异常的类
@ApiModel:描述一个Model的信息(一般用在请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:描述一个model的属性

启动项目,浏览器打开http://localhost:8080/swagger-ui.html

效果图

Springboot中使用swaggerUI_第2张图片
点击Expand Operations可以进行调试
Springboot中使用swaggerUI_第3张图片
我们就来查询一下,点击Try it out进行
Springboot中使用swaggerUI_第4张图片

学习文档

你可能感兴趣的:(#,SpringBoot)