更新
This commit is contained in:
@@ -15,48 +15,47 @@ import java.util.Map;
|
|||||||
* Spring Boot 自定义异常处理
|
* Spring Boot 自定义异常处理
|
||||||
* 所有的异常都派生自 Exception, 如果我们定义了某个异常的处理 Handler, Spring Boot 会调用用对应的异常 Handler, 否则会调用 Exception Handler.
|
* 所有的异常都派生自 Exception, 如果我们定义了某个异常的处理 Handler, Spring Boot 会调用用对应的异常 Handler, 否则会调用 Exception Handler.
|
||||||
* 有一个前提是在 application.yml 中定义两个属性, 让 springboot 在没有找到 url 的处理器触发异常; 让 springboot 不要自作多情加 /error 这个 map
|
* 有一个前提是在 application.yml 中定义两个属性, 让 springboot 在没有找到 url 的处理器触发异常; 让 springboot 不要自作多情加 /error 这个 map
|
||||||
* mvc:
|
* mvc:
|
||||||
* throw-exception-if-no-handler-found: true
|
* throw-exception-if-no-handler-found: true
|
||||||
* web:
|
* web:
|
||||||
* resources:
|
* resources:
|
||||||
* add-mappings: false
|
* add-mappings: false
|
||||||
* 只有如此, springboot 才会触发异常。
|
* 只有如此, springboot 才会触发异常。
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
@ControllerAdvice
|
@ControllerAdvice
|
||||||
public class ErrorController {
|
public class ErrorController {
|
||||||
|
|
||||||
@ExceptionHandler(Exception.class)
|
@ExceptionHandler(Exception.class)
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResponseEntity error(Exception ex) {
|
public ResponseEntity error(Exception ex) {
|
||||||
System.out.println("ErrorController.error Exception");
|
System.out.println("ErrorController.error Exception");
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
map.put("Exception", ex.getClass().getName());
|
map.put("Exception", ex.getClass().getName());
|
||||||
map.put("message", ex.getMessage());
|
map.put("message", ex.getMessage());
|
||||||
map.put("localizedMessage", ex.getLocalizedMessage());
|
map.put("localizedMessage", ex.getLocalizedMessage());
|
||||||
map.put("toString", ex.toString());
|
map.put("toString", ex.toString());
|
||||||
|
|
||||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(R.failed(map, "发生异常 "+ex.getMessage()));
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(R.failed(map, "发生异常 " + ex.getMessage()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ExceptionHandler(value = {NoHandlerFoundException.class})
|
@ExceptionHandler(value = {NoHandlerFoundException.class})
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ResponseEntity error(NoHandlerFoundException ex) {
|
public ResponseEntity error(NoHandlerFoundException ex) {
|
||||||
//System.out.println("ErrorController.error NoHandlerFoundException");
|
//System.out.println("ErrorController.error NoHandlerFoundException");
|
||||||
//ex.printStackTrace();
|
//ex.printStackTrace();
|
||||||
// //ex.getRawStatusCode()
|
// //ex.getRawStatusCode()
|
||||||
Map<String, Object> map = new HashMap<>();
|
Map<String, Object> map = new HashMap<>();
|
||||||
map.put("Exception", ex.getClass().getName());
|
map.put("Exception", ex.getClass().getName());
|
||||||
map.put("message", ex.getMessage());
|
map.put("message", ex.getMessage());
|
||||||
map.put("localizedMessage", ex.getLocalizedMessage());
|
map.put("localizedMessage", ex.getLocalizedMessage());
|
||||||
map.put("requestURL", ex.getRequestURL());
|
map.put("requestURL", ex.getRequestURL());
|
||||||
map.put("httpMethod", ex.getHttpMethod());
|
map.put("httpMethod", ex.getHttpMethod());
|
||||||
// map.put("cause", ex.getCause().toString());
|
// map.put("cause", ex.getCause().toString());
|
||||||
map.put("toString", ex.toString());
|
map.put("toString", ex.toString());
|
||||||
// map.put("comments", "单独的 ExceptionHandler, 系统管理捕获的全局异常 NoHandlerFoundException");
|
// map.put("comments", "单独的 ExceptionHandler, 系统管理捕获的全局异常 NoHandlerFoundException");
|
||||||
// //return map;
|
// //return map;
|
||||||
// //ResponseEntity<Map<String,Object>> r = new ResponseEntity<Map<String,Object>>(map, HttpStatus.INTERNAL_SERVER_ERROR);
|
// //ResponseEntity<Map<String,Object>> r = new ResponseEntity<Map<String,Object>>(map, HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
@@ -64,8 +63,8 @@ ex.printStackTrace();
|
|||||||
// //return r;
|
// //return r;
|
||||||
// return ResponseEntity.status(HttpStatus.NOT_FOUND).body(map);
|
// return ResponseEntity.status(HttpStatus.NOT_FOUND).body(map);
|
||||||
|
|
||||||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(R.failed(map, "没有找到"));
|
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(R.failed(map, "没有找到"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
+8
-1
@@ -1,5 +1,6 @@
|
|||||||
package digital.laboratory.platform.entrustment.controller;
|
package digital.laboratory.platform.entrustment.controller;
|
||||||
|
|
||||||
|
import digital.laboratory.platform.common.core.exception.ValidateCodeException;
|
||||||
import digital.laboratory.platform.common.core.util.R;
|
import digital.laboratory.platform.common.core.util.R;
|
||||||
import digital.laboratory.platform.entrustment.entity.EntrustmentIdentificationMaterial;
|
import digital.laboratory.platform.entrustment.entity.EntrustmentIdentificationMaterial;
|
||||||
import digital.laboratory.platform.entrustment.service.AcceptService;
|
import digital.laboratory.platform.entrustment.service.AcceptService;
|
||||||
@@ -25,7 +26,13 @@ public class AcceptController {
|
|||||||
@PutMapping("/alertMaterialAcceptNo")
|
@PutMapping("/alertMaterialAcceptNo")
|
||||||
@PreAuthorize("@pms.hasPermission('EntrustmentAccept')")
|
@PreAuthorize("@pms.hasPermission('EntrustmentAccept')")
|
||||||
public R alertMaterialAcceptNo(@RequestBody EntrustmentIdentificationMaterial material) {
|
public R alertMaterialAcceptNo(@RequestBody EntrustmentIdentificationMaterial material) {
|
||||||
Boolean success = acceptService.alertMaterialAcceptNo(material);
|
Boolean success = null;
|
||||||
|
try {
|
||||||
|
success = acceptService.alertMaterialAcceptNo(material);
|
||||||
|
} catch (ValidateCodeException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return R.failed(e.getMessage());
|
||||||
|
}
|
||||||
return R.ok(success);
|
return R.ok(success);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -49,7 +49,7 @@ public class AcceptServiceImpl implements AcceptService {
|
|||||||
* ^ 和 $:表示字符串的开始和结束
|
* ^ 和 $:表示字符串的开始和结束
|
||||||
*/
|
*/
|
||||||
String regex = "^\\d+-\\d+-\\d+$";
|
String regex = "^\\d+-\\d+-\\d+$";
|
||||||
if (Pattern.matches(regex, materialAcceptNo)) {
|
if (!Pattern.matches(regex, materialAcceptNo)) {
|
||||||
throw new ValidateCodeException(String.format("%s 不符合检材受理编号的规则!", materialAcceptNo));
|
throw new ValidateCodeException(String.format("%s 不符合检材受理编号的规则!", materialAcceptNo));
|
||||||
}
|
}
|
||||||
// 校验当前修改的检材受理编号对应的委托的受理编号存不存在, 委托的受理编号是检材受理编号截取最后一个横杠的内容
|
// 校验当前修改的检材受理编号对应的委托的受理编号存不存在, 委托的受理编号是检材受理编号截取最后一个横杠的内容
|
||||||
|
|||||||
Reference in New Issue
Block a user