错误处理,工具拓展
This commit is contained in:
parent
c0b36b423e
commit
1a22e1daec
22
src/main/java/com/jsl/oa/controllers/CustomController.java
Normal file
22
src/main/java/com/jsl/oa/controllers/CustomController.java
Normal file
@ -0,0 +1,22 @@
|
||||
package com.jsl.oa.controllers;
|
||||
|
||||
import com.jsl.oa.utils.BaseResponse;
|
||||
import com.jsl.oa.utils.ResultUtil;
|
||||
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class CustomController implements ErrorController {
|
||||
|
||||
@RequestMapping("/")
|
||||
public BaseResponse index() {
|
||||
return ResultUtil.success("欢迎使用JSL-OA系统,服务器处于正常状态");
|
||||
}
|
||||
|
||||
@RequestMapping("/error")
|
||||
public ResponseEntity<BaseResponse> handleError() {
|
||||
return ResultUtil.error("PageNotFound", 404, "请求资源不存在");
|
||||
}
|
||||
}
|
17
src/main/java/com/jsl/oa/exception/ProcessException.java
Normal file
17
src/main/java/com/jsl/oa/exception/ProcessException.java
Normal file
@ -0,0 +1,17 @@
|
||||
package com.jsl.oa.exception;
|
||||
|
||||
import com.jsl.oa.utils.BaseResponse;
|
||||
import com.jsl.oa.utils.ResultUtil;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.HttpRequestMethodNotSupportedException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
|
||||
@ControllerAdvice
|
||||
public class ProcessException {
|
||||
|
||||
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
|
||||
public ResponseEntity<BaseResponse> methodNotAllowedException() {
|
||||
return ResultUtil.error("MethodNotAllowed", 405, "请求方法错误");
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
package com.jsl.oa.utils;
|
||||
|
||||
import org.springframework.http.ResponseEntity;
|
||||
|
||||
public class ResultUtil {
|
||||
|
||||
public static BaseResponse success() {
|
||||
@ -25,4 +27,13 @@ public class ResultUtil {
|
||||
public static BaseResponse error(ErrorCode errorCode, Object data) {
|
||||
return new BaseResponse(errorCode.getOutput(), errorCode.getCode(), errorCode.getMessage(), data);
|
||||
}
|
||||
|
||||
public static BaseResponse error(String output, Integer code, String message, Object data) {
|
||||
return new BaseResponse(output, code, message, data);
|
||||
}
|
||||
|
||||
public static ResponseEntity<BaseResponse> error(String output, Integer code, String message) {
|
||||
return ResponseEntity.status(code)
|
||||
.body(new BaseResponse(output, code, message));
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user