
de.knightsoftnet.validators.server.controller.RestErrorHandler Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gwt-bean-validators-spring-gwtp Show documentation
Show all versions of gwt-bean-validators-spring-gwtp Show documentation
The GWT Bean Validators is a collection of JSR-303/JSR-349/JSR 380 bean validators. It can be used on server
and with the help of GWT even on client side. This packages contains rest services and validator
for phone numbers based on gwtp-rest dispatcher and spring to reduce client side code.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License. You may obtain a
* copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package de.knightsoftnet.validators.server.controller;
import de.knightsoftnet.validators.shared.data.ValidationDto;
import de.knightsoftnet.validators.shared.data.ValidationResultData;
import de.knightsoftnet.validators.shared.data.ValidationResultInterface;
import de.knightsoftnet.validators.shared.data.ValidationValueInterface;
import org.springframework.http.HttpStatus;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import java.util.List;
/**
* This error handler handles validation errors.
*
* @author Manfred Tremmel
*/
@ControllerAdvice
public class RestErrorHandler {
/**
* handle validation errors.
*
* @param pexception exception which is thrown when data is invalid.
* @return ValidationResultData with validation errors
*/
@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ResponseBody
public ValidationResultInterface processValidationError(
final MethodArgumentNotValidException pexception) {
final BindingResult result = pexception.getBindingResult();
final List fieldErrors = result.getFieldErrors();
return this.processFieldErrors(fieldErrors);
}
private ValidationResultInterface processFieldErrors(final List pfieldErrors) {
final ValidationResultInterface validationResult = new ValidationResultData();
for (final FieldError fieldError : pfieldErrors) {
final ValidationValueInterface validationDto = new ValidationDto();
validationDto.setPropertyPath(fieldError.getField());
validationDto.setMessage(fieldError.getDefaultMessage());
validationResult.getValidationErrorSet().add(validationDto);
}
return validationResult;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy