use of cn.taketoday.web.bind.MethodArgumentNotValidException in project today-infrastructure by TAKETODAY.
the class RequestPartMethodArgumentResolver method resolveArgument.
@Nullable
@Override
public Object resolveArgument(RequestContext context, ResolvableMethodParameter resolvable) throws Throwable {
MethodParameter parameter = resolvable.getParameter();
RequestPart requestPart = parameter.getParameterAnnotation(RequestPart.class);
boolean isRequired = ((requestPart == null || requestPart.required()) && !parameter.isOptional());
String name = getPartName(parameter, requestPart);
parameter = parameter.nestedIfOptional();
Object arg = null;
Object mpArg = MultipartResolutionDelegate.resolveMultipartArgument(name, parameter, context);
if (mpArg != MultipartResolutionDelegate.UNRESOLVABLE) {
arg = mpArg;
} else {
try {
var inputMessage = new RequestPartServletServerHttpRequest(context, name);
arg = readWithMessageConverters(inputMessage, parameter, parameter.getNestedGenericParameterType());
BindingContext binderFactory = context.getBindingContext();
if (binderFactory != null) {
WebDataBinder binder = binderFactory.createBinder(context, arg, name);
if (arg != null) {
validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
}
}
binderFactory.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
}
} catch (MissingRequestPartException | MultipartException ex) {
if (isRequired) {
throw ex;
}
}
}
if (arg == null && isRequired) {
if (!context.isMultipart()) {
throw new MultipartException("Current request is not a multipart request");
} else {
throw new MissingRequestPartException(name);
}
}
return adaptArgumentIfNecessary(arg, parameter);
}
use of cn.taketoday.web.bind.MethodArgumentNotValidException in project today-infrastructure by TAKETODAY.
the class ErrorResponseExceptionTests method methodArgumentNotValidException.
@Test
void methodArgumentNotValidException() {
BindingResult bindingResult = new BindException(new Object(), "object");
bindingResult.addError(new FieldError("object", "field", "message"));
ErrorResponse ex = new MethodArgumentNotValidException(this.methodParameter, bindingResult);
assertStatus(ex, HttpStatus.BAD_REQUEST);
assertDetail(ex, "Invalid request content.");
assertThat(ex.getHeaders()).isEmpty();
}
use of cn.taketoday.web.bind.MethodArgumentNotValidException in project today-framework by TAKETODAY.
the class ResponseEntityExceptionHandlerTests method methodArgumentNotValid.
@Test
public void methodArgumentNotValid() throws Exception {
Exception ex = new MethodArgumentNotValidException(new MethodParameter(getClass().getDeclaredMethod("handle", String.class), 0), new MapBindingResult(Collections.emptyMap(), "name"));
testException(ex);
}
use of cn.taketoday.web.bind.MethodArgumentNotValidException in project today-framework by TAKETODAY.
the class DefaultErrorAttributesTests method withMethodArgumentNotValidExceptionBindingErrors.
@Test
void withMethodArgumentNotValidExceptionBindingErrors() {
Method method = ReflectionUtils.findMethod(String.class, "substring", int.class);
MethodParameter parameter = new MethodParameter(method, 0);
BindingResult bindingResult = new MapBindingResult(Collections.singletonMap("a", "b"), "objectName");
bindingResult.addError(new ObjectError("c", "d"));
Exception ex = new MethodArgumentNotValidException(parameter, bindingResult);
testBindingResult(bindingResult, ex, ErrorAttributeOptions.of(Include.MESSAGE, Include.BINDING_ERRORS));
}
use of cn.taketoday.web.bind.MethodArgumentNotValidException in project today-framework by TAKETODAY.
the class ErrorResponseExceptionTests method methodArgumentNotValidException.
@Test
void methodArgumentNotValidException() {
BindingResult bindingResult = new BindException(new Object(), "object");
bindingResult.addError(new FieldError("object", "field", "message"));
ErrorResponse ex = new MethodArgumentNotValidException(this.methodParameter, bindingResult);
assertStatus(ex, HttpStatus.BAD_REQUEST);
assertDetail(ex, "Invalid request content.");
assertThat(ex.getHeaders()).isEmpty();
}
Aggregations