Springboot2整合knife4j过程解析

2020-03-11 16:01:34于丽

4.模型bean

package com.example.knife4j.demo.beans;

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

/**
 * 创建时间: 23:09 2018/9/19
 * 修改时间:
 * 编码人员: ZhengQf
 * 版  本: 0.0.1
 * 功能描述:
 */
@ApiModel(value = "用户模型")
public class UserEntity {
  @ApiModelProperty(value="id" ,required= true,example = "123")
  private Integer id;
  @ApiModelProperty(value="用户姓名" ,required=true,example = "郑钦锋")
  private String name;


  public Integer getId() {
    return id;
  }

  public void setId(Integer id) {
    this.id = id;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  @Override
  public String toString() {
    return "DemoDoctor [id=" + id + ", name=" + name + "]";
  }

}

5.两个接口controller

package com.example.knife4j.demo.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(value = "IndexController测试接口")
@RestController
public class IndexController {
  @ApiOperation(value = "测试index接口", nickname = "测试IndexController的index接口")
  @GetMapping("/index")
  public String index() {
    return "测试IndexController的index接口...";
  }

}
package com.example.knife4j.demo.controller;

import com.example.knife4j.demo.beans.UserEntity;
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.*;

@Api(value = "用户接口")
@RestController
public class UserController {


  @ApiOperation(value = "获取用户信息接口", nickname = "根据用户ID获取用户相关信息")
  @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "int")
  @PostMapping("/postMember")
  public UserEntity postMember(@RequestParam Integer id) {
    UserEntity userEntity = new UserEntity();
    userEntity.setId(id);
    userEntity.setName("admin");
    return userEntity;
  }


  @ApiOperation(value = "添加用户", nickname = "添加用户接口1", notes = "入参是复杂对象", produces = "application/json")
  @PostMapping("/postUser")
  @ResponseBody
  @ApiImplicitParam(paramType = "query", name = "userId", value = "用户id", required = true, dataType = "int")
  public UserEntity postUser(@RequestBody UserEntity user, @RequestParam("userId") int userId) { // 这里用包装类竟然报错
    if (user.getId() == userId) {
      return user;
    }
    return new UserEntity();
  }


  @ApiOperation(value = "添加用户", nickname = "添加用户接口2", notes = "入参是简单对象", produces = "application/json")
  @PostMapping("/addUser")
  @ResponseBody
  @ApiImplicitParams({
      @ApiImplicitParam(paramType = "query", name = "userName", value = "用户姓名", required = true, dataType = "String"),
      @ApiImplicitParam(paramType = "query", name = "id", value = "用户id", required = true, dataType = "int")})
  public UserEntity addUser(String userName, int id) {
    UserEntity userEntity = new UserEntity();
    userEntity.setName(userName);
    userEntity.setId(id);
    return userEntity;
  }

}