Spring-Swagger使用

全局配置

package com.qifeng.springswagger.config;

import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author zengqifeng
 * @version 1.0
 * @date 2019/12/13 16:24
 */
@Component
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.qifeng.springswagger.controller"))
                .paths(PathSelectors.any())
                .build().apiInfo(new ApiInfoBuilder()
                        .title("SpringBoot整合Swagger")
                        .description("SpringBoot整合Swagger,详细信息......")
                        .version("1.0")
                        .contact(new Contact("qifeng", "haylion.cn", "zengqifengcute@163.com"))
                        .license("The Apache License")
                        .licenseUrl("http://www.baidu.com")
                        .build());
    }
}

Controller

package com.qifeng.springswagger.controller;

import com.qifeng.springswagger.entity.Student;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zengqifeng
 * @version 1.0
 * @date 2019/12/13 16:31
 */
@Api("学生相关接口")
@RestController
@RequestMapping("Student")
public class StudentController {
    /**
     * 单个参数、多个参数     @RequestBody类型参数(json格式)
     * response如何处理?
      * @param id
     * @return
     */

    /**
     * 关于Swagger几大API的使用
     * @Api("学生相关接口")       --controller
     * @ApiOperation("根据id查询student")       --method
     * @ApiImplicitParam(name = "id", value = "编号", defaultValue = "1", required = true, dataType = "int(11)")
     *      单个参数
     * @ApiImplicitParams({
     *             @ApiImplicitParam(name = "id", value = "编号", defaultValue = "1", required = true, dataType = "Integer"),
     *             @ApiImplicitParam(name = "username", value = "姓名", defaultValue = "二哈", required = true, dataType = "String")
     *     })
     *      多个参数
     * @ApiModel
     * @ApiModelProperty("student Id")
     * @RequestBody类型参数
     *
     */

    //单个参数
    @ApiOperation("根据id查询student")
    @ApiImplicitParam(name = "id", value = "编号", defaultValue = "1", required = true, dataType = "int(11)")
    @GetMapping("getStudentInfoById")
    public Student getStudentInfoById(Integer id) {
        Student student = new Student();
        student.setId(1);
        student.setUsername("二哈");
        student.setPassword("123");
        return student;
    }

    //多个参数
    @ApiOperation("根据id和username查询student")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编号", defaultValue = "1", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "username", value = "姓名", defaultValue = "二哈", required = true, dataType = "String")
    })
    @GetMapping("getStudentInfoByIdAndUsername")
    public Student getStudentInfoByIdAndUsername(Integer id, String username) {
        Student student = new Student();
        student.setId(1);
        student.setUsername("二哈");
        student.setPassword("123");
        return student;
    }


    //requestBody类型参数  其文档注释写在bean类中
    @ApiOperation("新增student")
    @PostMapping("insertStudent")
    public Student insertStudent(@RequestBody Student student) {

        return student;
    }
}

Entity(@RequestBody 类型参数)

package com.qifeng.springswagger.entity;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @author zengqifeng
 * @version 1.0
 * @date 2019/12/13 16:23
 */
@ApiModel
@Data
public class Student {
    @ApiModelProperty("student Id")
    private Integer id;
    @ApiModelProperty("student 姓名")
    private String username;
    @ApiModelProperty("student 密码")
    private String password;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值