springboot 接口返回 ResponseEntity实现按照一定格式返回结果

以创建目录接口为例

@ApiOperation(value = "创建目录", notes = "在某目录下创建新文件夹")
    @ApiResponses({
            @ApiResponse(code = 500, response = RestCodeMsg.class, message = "错误")
    })
    @PostMapping(value = "api/scene/createdir")
    public ResponseEntity createNewOrEditFile(@RequestBody ixviewVo ixveVo) {
        String name = ixveVo.getDirname();
        log.info("Namelength:" + name.length());
        if (name.length() > 255) {
            throw new ServiceException(ErrorCode.FILE_NAME_IS_TOO_LONG, "文件夹名称长度不能超过255字节");
        }
        try {
            boolean flag = ixviewservice.createNewOrEditFile(ixveVo);
            if (flag) {
                Map dirmap = new HashMap<>();
                if (ixveVo.getParentId() == 0) {
                    dirmap.put("dirid", ixviewservice.findidirdnoparent(ixveVo.getDirname()));
                } else {
                    dirmap.put("dirid", ixviewservice.findidird(ixveVo.getDirname(), ixveVo.getParentId()));
                }
                dirmap.put("dirname", ixveVo.getDirname());
                dirmap.put("parentid", ixveVo.getParentId());
                return ResponseEntity.ok(getPublicBackValueDir(flag, dirmap));
            } else {
                return ResponseEntity.ok(getPublicBackValue(flag, "创建失败"));
            }
        } catch (IOException e) {
            log.error(e.getMessage());
            throw new MyServiceException(RestCodeMsg.CREATNEW_OR_EDIT_FILE_ERROR, e);
        }
    }
public Map getPublicBackValue(boolean flag, String message) {
        Map map = new HashMap();
        if (flag) {
            map.put("result_code", 0);
        } else {
            map.put("result_code", 1);
        }
        map.put("result_reason", message);
        return map;
    }

根据返回值进行整形后返回。

你可能感兴趣的:(springboot 接口返回 ResponseEntity实现按照一定格式返回结果)