Skip to content

Commit fc2a38c

Browse files
committed
code-review
1 parent 0ac52c4 commit fc2a38c

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

core-web/src/main/java/lazy/fast/code/core/web/exception/AbstractRestExceptionHandler.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import lazy.fast.code.core.web.result.MsgEnum;
44
import lazy.fast.code.core.web.result.ResultMsg;
55
import lombok.extern.slf4j.Slf4j;
6-
import org.springframework.beans.TypeMismatchException;
76
import org.springframework.http.HttpStatus;
87
import org.springframework.http.ResponseEntity;
98
import org.springframework.validation.BindException;
@@ -16,12 +15,14 @@
1615
import org.springframework.web.bind.MissingServletRequestParameterException;
1716
import org.springframework.web.bind.annotation.ExceptionHandler;
1817
import org.springframework.web.client.RestClientException;
18+
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
1919

2020
import java.util.List;
2121

2222
/**
2323
* 全局异常处理器 - 只针对返回JSON数据交互格式(标记为@RestController类) <br />
2424
* 应用应该创建一个自定义全局异常处理器,并继承此类,让此类定义的ExceptionHandler生效,如:
25+
*
2526
* <pre>
2627
* &#64;RestControllerAdvice
2728
* public class GlobalRestExceptionHandler extends AbstractRestExceptionHandler {
@@ -86,10 +87,11 @@ public ResponseEntity<ResultMsg> illegalStateExceptionHandler(IllegalStateExcept
8687
return ResponseEntity.badRequest().body(ResultMsg.fail(e.getParameterName() + "参数缺失"));
8788
}
8889

89-
@ExceptionHandler(value = TypeMismatchException.class)
90-
public ResponseEntity<ResultMsg> typeMismatchExceptionHandler(TypeMismatchException e) {
90+
@ExceptionHandler(value = MethodArgumentTypeMismatchException.class)
91+
public ResponseEntity<ResultMsg> typeMismatchExceptionHandler(MethodArgumentTypeMismatchException e) {
92+
assert e.getRequiredType() != null;
9193
return ResponseEntity.badRequest()
92-
.body(ResultMsg.fail("参数类型不匹配,参数" + e.getPropertyName() + "类型应该为" + e.getRequiredType()));
94+
.body(ResultMsg.fail("参数类型不匹配,参数" + e.getName() + "类型应该为" + e.getRequiredType().getSimpleName()));
9395
}
9496

9597
@ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)

0 commit comments

Comments
 (0)