暂存提交

This commit is contained in:
2026-07-31 14:55:34 +08:00
parent bc3c18a6e9
commit 7a19e2f096
28 changed files with 655 additions and 46 deletions
@@ -15,6 +15,7 @@ import org.springframework.validation.FieldError;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@@ -54,7 +55,8 @@ public class GlobalExceptionHandler {
return validationResponse(errors, request);
}
@ExceptionHandler({HttpMessageNotReadableException.class, MissingServletRequestParameterException.class})
@ExceptionHandler({HttpMessageNotReadableException.class, MissingServletRequestParameterException.class,
MethodArgumentTypeMismatchException.class})
public ResponseEntity<ApiResponse<Void>> handleBadRequest(Exception exception, HttpServletRequest request) {
return failure(ResultCode.BAD_REQUEST, ResultCode.BAD_REQUEST.message(), request);
}
@@ -4,6 +4,10 @@ import com.baiyue.common.core.api.ResultCode;
import com.baiyue.common.core.exception.BizException;
import org.junit.jupiter.api.Test;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.core.MethodParameter;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import java.lang.reflect.Method;
import static org.assertj.core.api.Assertions.assertThat;
@@ -20,4 +24,21 @@ class GlobalExceptionHandlerTest {
assertThat(response.getBody().code()).isEqualTo(ResultCode.BUSINESS_ERROR.code());
assertThat(response.getBody().requestId()).isEqualTo("r1");
}
@Test
void 查询参数类型错误映射为四百而不是五百() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
Method method = GlobalExceptionHandlerTest.class.getDeclaredMethod("paging", long.class);
MethodParameter parameter = new MethodParameter(method, 0);
MethodArgumentTypeMismatchException exception = new MethodArgumentTypeMismatchException(
"abc", Long.class, "page", parameter, new NumberFormatException("abc"));
var response = handler.handleBadRequest(exception, request);
assertThat(response.getStatusCode().value()).isEqualTo(400);
assertThat(response.getBody().code()).isEqualTo(ResultCode.BAD_REQUEST.code());
}
private void paging(long page) {
}
}