我有一个休息控制器类,如下所示,当用户对象无效时,它会抛出自定义异常。但是这些异常被 spring 框架包装,而不是由控制器建议中定义的特定异常处理程序处理。
@PostMapping
public ResponseEntity<String> process(
@PathVariable(User_id) String id,
@Valid @RequestBody(required = false) User user) {
}
Run Code Online (Sandbox Code Playgroud)
例如,Json 映射异常被包装为 org.springframework.http.converter.HttpMessageNotReadableException,并且不会通过控制器建议中的以下异常处理程序进行处理。
@ExceptionHandler(JsonMappingException.class)
public ResponseEntity<ErrorMessage> handleJsonMappingException(JsonMappingException e){
//process to return error message
}
Run Code Online (Sandbox Code Playgroud)
如何处理构建请求主体对象时抛出的这些特定异常?
实际收到的异常:
2018-09-16 23:14:07,671 ERROR [qtp1824013753-23] c.m.a.c.e.m.MyExceptionHandler [MyExceptionHandler.java:111] Unhandled exception : Type definition error: [simple type, class com.common.model.User]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.common.model.User`, problem: '123ghijk' is not a valid DeptName
at [Source: UNKNOWN; line: -1, column: -1] (through reference chain: com.common.model.Employee["DeptName"])
org.springframework.http.converter.HttpMessageConversionException: Type definition …Run Code Online (Sandbox Code Playgroud) exception spring-mvc spring-boot spring-restcontroller spring-rest