外层对象封装
This commit is contained in:
@@ -9,8 +9,11 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import top.fjy8018.fileupload.VO.FileInfoVO;
|
||||
import top.fjy8018.fileupload.VO.ResultVO;
|
||||
import top.fjy8018.fileupload.dataobject.es.EsFileInfo;
|
||||
import top.fjy8018.fileupload.enums.ResultVOEnum;
|
||||
import top.fjy8018.fileupload.service.FileService;
|
||||
import top.fjy8018.fileupload.util.ResultVOUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -27,9 +30,9 @@ public class FileSearchController {
|
||||
private FileService fileService;
|
||||
|
||||
@GetMapping("/findbyname")
|
||||
public List<FileInfoVO> searchByFileName(@RequestParam String fileName,
|
||||
@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer page,
|
||||
@RequestParam(name = "size", required = false, defaultValue = "5") Integer size){
|
||||
public ResultVO searchByFileName(@RequestParam String fileName,
|
||||
@RequestParam(name = "pageNum", required = false, defaultValue = "1") Integer page,
|
||||
@RequestParam(name = "size", required = false, defaultValue = "5") Integer size){
|
||||
|
||||
PageRequest pageRequest = PageRequest.of(page - 1, size);
|
||||
Page<EsFileInfo> productInfoPage = fileService.findByFileName(pageRequest,fileName);
|
||||
@@ -43,6 +46,6 @@ public class FileSearchController {
|
||||
fileInfoVOList.add(fileInfoVO);
|
||||
}
|
||||
|
||||
return fileInfoVOList;
|
||||
return ResultVOUtil.data(ResultVOEnum.SUCCESS,fileInfoVOList);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,13 @@ package top.fjy8018.fileupload.enums;
|
||||
* @date 2018-06-22 09:28
|
||||
*/
|
||||
public enum ResultVOEnum implements CodeEnum {
|
||||
UPLOAD_SUCCESS(0,"上传成功"),
|
||||
UPLOAD_FAIL(-1,"上传失败"),
|
||||
SUCCESS(1,"成功"),
|
||||
UNKOWN_ERROR(-1,"系统异常"),
|
||||
FORBIDDEN_ACCES(1,"没有访问权限"),
|
||||
LOG_OUT(2,"未登录"),
|
||||
USER_NOT_EXIST(3,"用户不存在"),
|
||||
UPLOAD_SUCCESS(4,"上传成功"),
|
||||
UPLOAD_FAIL(5,"上传失败"),
|
||||
;
|
||||
|
||||
private Integer code;
|
||||
|
||||
@@ -5,6 +5,7 @@ import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import top.fjy8018.fileupload.VO.ResultVO;
|
||||
import top.fjy8018.fileupload.enums.ResultVOEnum;
|
||||
import top.fjy8018.fileupload.exception.FileUploadException;
|
||||
import top.fjy8018.fileupload.util.ResultVOUtil;
|
||||
|
||||
@@ -17,9 +18,15 @@ import top.fjy8018.fileupload.util.ResultVOUtil;
|
||||
@ControllerAdvice
|
||||
public class ExceptionHandle {
|
||||
|
||||
@ExceptionHandler(value = Exception.class)
|
||||
@ResponseBody
|
||||
@ExceptionHandler(value = FileUploadException.class)
|
||||
public ResultVO handle(FileUploadException e){
|
||||
return ResultVOUtil.error(e.getCode(),e.getMessage());
|
||||
public ResultVO handle(Exception e){
|
||||
if (e instanceof FileUploadException){
|
||||
FileUploadException userException = (FileUploadException)e;
|
||||
return ResultVOUtil.error(userException.getCode(),userException.getMessage());
|
||||
}else{
|
||||
log.error("系统异常",e);
|
||||
return ResultVOUtil.error(ResultVOEnum.UNKOWN_ERROR.getCode(),ResultVOEnum.UNKOWN_ERROR.getMsg());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package top.fjy8018.fileupload.service;
|
||||
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.stereotype.Service;
|
||||
import top.fjy8018.fileupload.dataobject.es.EsFileInfo;
|
||||
|
||||
public interface FileService {
|
||||
|
||||
@@ -42,4 +42,13 @@ public class ResultVOUtil {
|
||||
resultVO.setMsg(msg);
|
||||
return resultVO;
|
||||
}
|
||||
|
||||
public static ResultVO data(ResultVOEnum resultVOEnum,Object object){
|
||||
ResultVO resultVO = new ResultVO();
|
||||
|
||||
resultVO.setCode(resultVOEnum.getCode());
|
||||
resultVO.setMsg(resultVOEnum.getMsg());
|
||||
resultVO.setData(object);
|
||||
return resultVO;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user