首页>代码>Swagger的简单案例,适合初级者学习使用>/SwaggerTest/src/main/java/cn/jbit/controller/UserController.java
package cn.jbit.controller;

import cn.jbit.entity.ResponseBean;
import cn.jbit.entity.User;
import cn.jbit.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Map;

@RestController
@RequestMapping("/user")
@Validated
@Api(tags = "用户信息管理")
public class UserController {
    @Autowired
    private UserService userService;


    @ApiOperation(value = "查询用户信息列表",notes = "分页查询用户信息列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum",value = "当前页",paramType = "query",defaultValue = "1",dataType = "int"),
            @ApiImplicitParam(name = "pageSize",value = "条数",paramType = "query",defaultValue = "5",dataType = "int"),
            @ApiImplicitParam(name = "name",value = "模糊查询用户名称",paramType = "query",dataType = "String"),
    })
    @GetMapping("/list-page")
    public ResponseBean lists(@RequestParam(defaultValue = "1", required = false) Integer pageNum,
                              @RequestParam(defaultValue = "5", required = false) Integer pageSize,
                              @ApiIgnore @RequestParam Map<String, String> conditionMap) {
        Page<User> page = userService.listPage(pageNum, pageSize, conditionMap);
        return ResponseBean.success(page.getContent());
    }

    @ApiOperation(value = "查询用户信息",notes = "通过ID查询用户信息")
    @ApiImplicitParam(name = "id",value = "主键Id",paramType = "path",dataType = "int",required = true)
    @GetMapping("/get/{id}")
    public ResponseBean findById(@NotNull(message = "主键ID不能为空") @PathVariable("id") Integer id) {
        try {
            User user = userService.getById(id);
            if (null == user) {
                return ResponseBean.failed(500, "用户信息不存在");
            }
            return ResponseBean.success(user);
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("查询用户信息发生异常。。。。。。。");
            return ResponseBean.failed(500);
        }
    }

    @ApiOperation(value = "新增用户信息,修改用户信息",notes = "根据ID修改,ID不存在添加")
    @PostMapping("/save")
    public ResponseBean save(@Validated User user) {
        try {
            userService.saveOrUpdate(user);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("保存用户信息发生异常。。。。。。。");
            return ResponseBean.failed(500);
        }
    }

    @ApiOperation(value = "删除用户信息",notes = "根据ID删除用户信息")
    @ApiImplicitParam(name = "id",value = "主键Id",paramType = "path",dataType = "int",required = true)
    @GetMapping("/delete/{id}")
    public ResponseBean delete(@NotNull(message = "主键ID不能为空") @PathVariable("id") Integer id) {
        try {
            userService.delete(id);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("删除发生异常。。。。。。。");
            return ResponseBean.failed(500);
        }
    }

    @ApiOperation(value = "批量删除",notes = "根据ID批量删除用户信息")
    @ApiImplicitParam(name = "ids",value = "主键id数组",paramType = "body",allowMultiple = true,required = true)
    @PostMapping("/delete")
    public ResponseBean deleteBatch(@RequestBody @NotEmpty(message = "id不能为空") Integer[] ids) {
        try {
            userService.deleteBatch(ids);
            return ResponseBean.success();
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("批量删除发生异常。。。。。。。");
            return ResponseBean.failed(500);
        }
    }

}
最近下载更多
微信网友_6368711690080256  LV3 2023年2月28日
1203174100  LV1 2023年2月22日
282363  LV1 2022年10月4日
cao1992  LV24 2022年7月18日
yijie110  LV5 2022年4月18日
gy12345  LV6 2022年2月15日
taskmaster  LV6 2022年1月2日
zcl02036716  LV17 2021年12月3日
123854216  LV5 2021年11月24日
hyz419  LV6 2021年9月18日
最近浏览更多
内心向阳  LV4 2023年11月7日
jiemomo  LV12 2023年10月19日
lingtiejing  LV15 2023年10月1日
siximu912  LV10 2023年9月19日
qq573914838  LV5 2023年9月2日
youwuzuichen  LV10 2023年8月14日
空心菜4  LV9 2023年7月16日
8376x123 2023年6月12日
暂无贡献等级
hwbhome 2023年6月4日
暂无贡献等级
wangyz  LV8 2023年5月17日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友