use of org.springframework.web.bind.MethodArgumentNotValidException in project spring-cloud-open-service-broker by spring-cloud.
the class BaseControllerTest method methodArgumentNotValidExceptionGivesExpectedStatus.
@Test
public void methodArgumentNotValidExceptionGivesExpectedStatus() throws NoSuchMethodException {
BindingResult bindingResult = new MapBindingResult(new HashMap<>(), "objectName");
bindingResult.addError(new FieldError("objectName", "field1", "message"));
bindingResult.addError(new FieldError("objectName", "field2", "message"));
Method method = this.getClass().getMethod("setUp", (Class<?>[]) null);
MethodParameter parameter = new MethodParameter(method, -1);
MethodArgumentNotValidException exception = new MethodArgumentNotValidException(parameter, bindingResult);
ResponseEntity<ErrorMessage> responseEntity = controller.handleException(exception);
assertThat(responseEntity.getStatusCode()).isEqualTo(HttpStatus.UNPROCESSABLE_ENTITY);
assertThat(responseEntity.getBody().getError()).isNull();
assertThat(responseEntity.getBody().getMessage()).contains("field1");
assertThat(responseEntity.getBody().getMessage()).contains("field2");
}
use of org.springframework.web.bind.MethodArgumentNotValidException in project jim-framework by jiangmin168168.
the class BaseController method exception.
@ExceptionHandler
public ValueResult exception(HttpServletRequest request, Exception ex) {
ValueResult result = new ValueResult();
if (ex instanceof ConstraintViolationException) {
ConstraintViolationException constraintViolationException = (ConstraintViolationException) ex;
Set<ConstraintViolation<?>> constraintViolations = constraintViolationException.getConstraintViolations();
String errorMsg = this.getConstraintViolationsMsg(constraintViolations);
result.setError(new ErrorInfo(ErrorDef.InvalidParameters.getCode(), errorMsg));
result.setResult(false);
} else if (ex instanceof MethodArgumentNotValidException) {
MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) ex;
List<ObjectError> allErrors = methodArgumentNotValidException.getBindingResult().getAllErrors();
String errorMsg = this.getAllParametersInValidMsg(allErrors);
result.setError(new ErrorInfo(ErrorDef.InvalidParameters.getCode(), errorMsg));
} else {
logger.error("system error", ex);
result.setError(new ErrorInfo(ErrorDef.ServerError.getCode(), ex.getMessage()));
result.setResult(false);
}
return result;
}
use of org.springframework.web.bind.MethodArgumentNotValidException in project spring-boot-jpa by ssherwood.
the class BootJpaApplication method errorAttributes.
/**
* Customized ErrorAttribute bean.
* We really need to find a cleaner way of handling these error messages.
*
* @return customized ErrorAttributes
*/
@Bean
public ErrorAttributes errorAttributes() {
return new DefaultErrorAttributes() {
@Override
public Map<String, Object> getErrorAttributes(final RequestAttributes requestAttributes, final boolean includeStackTrace) {
Map<String, Object> attributes = super.getErrorAttributes(requestAttributes, includeStackTrace);
Throwable error = getError(requestAttributes);
if (error instanceof MethodArgumentNotValidException) {
MethodArgumentNotValidException ex = ((MethodArgumentNotValidException) error);
attributes.put("errors", ex.getMessage());
}
return attributes;
}
};
}
use of org.springframework.web.bind.MethodArgumentNotValidException in project spring-framework by spring-projects.
the class RequestPartMethodArgumentResolver method resolveArgument.
@Override
@Nullable
public Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer, NativeWebRequest request, @Nullable WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest servletRequest = request.getNativeRequest(HttpServletRequest.class);
Assert.state(servletRequest != null, "No HttpServletRequest");
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, servletRequest);
if (mpArg != MultipartResolutionDelegate.UNRESOLVABLE) {
arg = mpArg;
} else {
try {
HttpInputMessage inputMessage = new RequestPartServletServerHttpRequest(servletRequest, name);
arg = readWithMessageConverters(inputMessage, parameter, parameter.getNestedGenericParameterType());
if (binderFactory != null) {
WebDataBinder binder = binderFactory.createBinder(request, arg, name);
if (arg != null) {
validateIfApplicable(binder, parameter);
if (binder.getBindingResult().hasErrors() && isBindExceptionRequired(binder, parameter)) {
throw new MethodArgumentNotValidException(parameter, binder.getBindingResult());
}
}
if (mavContainer != null) {
mavContainer.addAttribute(BindingResult.MODEL_KEY_PREFIX + name, binder.getBindingResult());
}
}
} catch (MissingServletRequestPartException | MultipartException ex) {
if (isRequired) {
throw ex;
}
}
}
if (arg == null && isRequired) {
if (!MultipartResolutionDelegate.isMultipartRequest(servletRequest)) {
throw new MultipartException("Current request is not a multipart request");
} else {
throw new MissingServletRequestPartException(name);
}
}
return adaptArgumentIfNecessary(arg, parameter);
}
use of org.springframework.web.bind.MethodArgumentNotValidException in project chassis by Kixeye.
the class RawWebSocketMessage method deserialize.
/**
* Deserializes the given message.
*
* @param action
* @return
* @throws Exception
*/
public T deserialize(WebSocketAction action) throws Exception {
// first deserialize
T message = null;
if (messageClass != null) {
message = serDe.deserialize(new ByteBufferBackedInputStream(rawData), messageClass);
}
// then validate
if (message != null && action.shouldValidatePayload()) {
SpringValidatorAdapter validatorAdapter = new SpringValidatorAdapter(messageValidator);
BeanPropertyBindingResult result = new BeanPropertyBindingResult(message, messageClass.getName());
validatorAdapter.validate(message, result);
if (result.hasErrors()) {
throw new MethodArgumentNotValidException(new MethodParameter(action.getMethod(), action.getPayloadParameterIndex()), result);
}
}
return message;
}
Aggregations