springboot整合swagger

1、swagger常用注解说明

 https://www.cnblogs.com/songjn/p/13396392.html

2、pom.xml中引用依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.8.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.8.0</version>
</dependency>

3、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;

/**
 * http://localhost:8090/test/swagger-ui.html
 * swagger2的配置内容仅仅就是需要创建一个Docket实例
 */
@Configuration
@EnableSwagger2 //启用swagger2
public class Swagger2Config {
//    @Value("${swagger.enable}")
//    private boolean enableSwagger;
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .apiInfo(apiInfo())
//                .enable(enableSwagger)//配置是否开启配置
                .select()
                //@ApiIgnore 这样,该接口就不会暴露在 swagger2 的页面下
                //.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))//2这里采用包含注解的方式来确定要显示的接口
                .apis(RequestHandlerSelectors.basePackage("com.dm.controller")) //2这里采用包扫描的方式来确定要显示的接口
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("2服务管理系统")
                .description("2基础服务包括【2字段管理模块2】【2接口管理模块2】【2mock服务模块2】【2接口标准化检测2】四个服务2")
                .version("0.0.1")
                .build(); // 这部分信息其实可以自定义到配置文件中读取
    }
}

4、使用注解后的Controller

import com.dm.model.Result;
import com.dm.model.StatusCode;
import com.dm.repository.entity.tbField;
import com.dm.service.tbFieldService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "tbFieldController",description = "Field相关接口")
@RestController
@RequestMapping("/tbField")
@CrossOrigin
public class tbFieldController {

    @Autowired
    private tbFieldService tbFieldService;

    /***
     * tbField分页条件搜索实现
     * @param tbField
     * @param page
     * @param size
     * @return
     */
    @ApiOperation(value = "tbField条件分页查询",notes = "分页条件查询tbField方法详情",tags = {"tbFieldController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/search/{page}/{size}" )
    public Result<List<tbField>> findPage(@RequestBody(required = false) @ApiParam(name = "tbField对象",value = "传入JSON数据",required = false) tbField tbField, @PathVariable  int page, @PathVariable  int size){
        //调用tbFieldService实现分页条件查询tbField
        List<tbField> tbFieldList = tbFieldService.findPage(tbField, page, size);
        return new Result(StatusCode.OK,"查询成功",tbFieldList);
    }

    /***
     * tbField分页搜索实现
     * @param page:当前页
     * @param size:每页显示多少条
     * @return
     */
    @ApiOperation(value = "tbField分页查询",notes = "分页查询tbField方法详情",tags = {"tbFieldController"})
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "page", value = "当前页", required = true, dataType = "Integer"),
            @ApiImplicitParam(paramType = "path", name = "size", value = "每页显示条数", required = true, dataType = "Integer")
    })
    @GetMapping(value = "/search/{page}/{size}" )
    public Result<List<tbField>> findPage(@PathVariable  int page, @PathVariable  int size){
        //调用tbFieldService实现分页查询tbField
        List<tbField> tbFieldList = tbFieldService.findPage(page, size);
        return new Result<>(StatusCode.OK,"查询成功",tbFieldList);
    }

    /***
     * 多条件搜索品牌数据
     * @param tbField
     * @return
     */
    @ApiOperation(value = "tbField条件查询",notes = "条件查询tbField方法详情",tags = {"tbFieldController"})
    @PostMapping(value = "/search" )
    public Result<List<tbField>> findList(@RequestBody(required = false) @ApiParam(name = "tbField对象",value = "传入JSON数据",required = false) tbField tbField){
        //调用tbFieldService实现条件查询tbField
        List<tbField> list = tbFieldService.findList(tbField);
        return new Result<List<tbField>>(StatusCode.OK,"查询成功",list);
    }

    /***
     * 根据ID删除品牌数据
     * @param id
     * @return
     */
    @ApiOperation(value = "tbField根据ID删除",notes = "根据ID删除tbField方法详情",tags = {"tbFieldController"})
    @DeleteMapping(value = "/{id}" )
    public Result delete(@PathVariable @ApiParam(value = "主键ID", required = true) Integer id){
        //调用tbFieldService实现根据主键删除
        tbFieldService.delete(id);
        return new Result(StatusCode.OK,"删除成功");
    }

    /***
     * 修改tbField数据
     * @param tbField
     * @param id
     * @return
     */
    @ApiOperation(value = "tbField根据ID修改",notes = "根据ID修改tbField方法详情",tags = {"tbFieldController"})
    @ApiImplicitParam(paramType = "path", name = "id", value = "主键ID", required = true, dataType = "Integer")
    @PutMapping(value="/{id}")
    public Result update(@RequestBody @ApiParam(name = "tbField对象",value = "传入JSON数据",required = false) tbField tbField,@PathVariable Integer id){
        //设置主键值
        tbField.setId(id);
        //调用tbFieldService实现修改tbField
        tbFieldService.update(tbField);
        return new Result(StatusCode.OK,"修改成功");
    }

    /***
     * 新增tbField数据
     * @param tbField
     * @return
     */
    @ApiOperation(value = "tbField添加",notes = "添加tbField方法详情",tags = {"tbFieldController"})
    @PostMapping
    public Result add(@RequestBody  @ApiParam(name = "tbField对象",value = "传入JSON数据",required = true) tbField tbField){
        //调用tbFieldService实现添加tbField
        tbFieldService.add(tbField);
        return new Result(StatusCode.OK,"添加成功");
    }

    /***
     * 根据ID查询tbField数据
     * @param id
     * @return
     */
    @ApiOperation(value = "tbField根据ID查询",notes = "根据ID查询tbField方法详情",tags = {"tbFieldController"})
    @GetMapping("/{id}")
    public Result<tbField> findById(@PathVariable @ApiParam(value = "主键ID", required = true) Integer id){
        //调用tbFieldService实现根据主键查询tbField
        tbField tbField = tbFieldService.findById(id);
        return new Result<tbField>(StatusCode.OK,"查询成功",tbField);
    }

}

5、访问地址

http://localhost:8090/test/swagger-ui.html

  

 

 

posted @ 2020-07-29 12:13  songjn  阅读(162)  评论(0编辑  收藏  举报