spring boot项目从零开始-(3)集成Swagger

简述

环境

  • Idea
  • log4j2 + swagger2

代码
https://gitee.com/ydfind/spring_boot_demo/tree/dev-swagger/

目录
在这里插入图片描述

步骤

pom.xml

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

或者

<!--
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
            <exclusions>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-annotations</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>io.swagger</groupId>
                    <artifactId>swagger-models</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-annotations</artifactId>
            <version>1.5.22</version>
        </dependency>
        <dependency>
            <groupId>io.swagger</groupId>
            <artifactId>swagger-models</artifactId>
            <version>1.5.22</version>
        </dependency>
        -->

若没有引入fastjson,则引入

        <!-- json -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.58</version>
        </dependency>

启用

在启动函数加入@EnableSwagger2

@EnableSwagger2
@SpringBootApplication
@RestController
public class MainApplication {

测试

UserRequestDto.java

package com.ydfind.start.controller.test.model;

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

/**
 * 用户请求参数类
 *
 * @author ydfind
 * @date 2020.1.5
 */
@Data
@ApiModel("用户类")
public class UserRequestDto {

    @ApiModelProperty(name = "id", value = "用户id", example = "1")
    private int id;

    @ApiModelProperty(name = "name", value = "用户名称", example = "张三")
    private String name;

    @ApiModelProperty(hidden = true)
    private int age;

}

TestController.java

package com.ydfind.start.controller.test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ydfind.start.controller.test.model.UserRequestDto;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * 试验controller
 *
 * @author ydfind
 * @date 2020.1.5
 */
@RestController
@Slf4j
@RequestMapping(value = "/api/v1/test", produces = MediaType.APPLICATION_JSON_VALUE)
public class TestController {

    @GetMapping("/swagger2/1")
    @ApiOperation("swagger2用例:单参数")
    @ApiImplicitParam(name = "param", value = "参数", defaultValue = "默认参数值")
    public String getSwagger1(String param) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("param", param);
        return jsonObject.toJSONString();
    }

    @GetMapping("/swagger2/2")
    @ApiOperation("swagger2用例:多参数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", defaultValue = "1"),
            @ApiImplicitParam(name = "param", value = "参数", defaultValue = "默认参数值")
    })
    public String getSwagger2(int id, String param) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("id", id);
        jsonObject.put("param", param);
        return jsonObject.toJSONString();
    }

    @GetMapping("/swagger2/3")
    @ApiOperation("swagger2用例:单对象参数")
    public String getSwagger3(UserRequestDto requestDto) {
        return JSON.toJSONString(requestDto);
    }

    @PostMapping("/swagger2/4")
    @ApiOperation("swagger2用例:单对象参数")
    public String getSwagger4(@RequestBody UserRequestDto requestDto) {
        return JSON.toJSONString(requestDto);
    }
}

结果

访问:http://localhost:8182/swagger-ui.html
在这里插入图片描述

  • 第一个接口测试
    在这里插入图片描述
    在这里插入图片描述
  • 第三个接口如下
    在这里插入图片描述
  • model
    在这里插入图片描述

备注

更复杂的,请参考:https://www.cnblogs.com/niunafei/p/11120274.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值