Example usage for org.springframework.validation FieldError getDefaultMessage

List of usage examples for org.springframework.validation FieldError getDefaultMessage

Introduction

In this page you can find the example usage for org.springframework.validation FieldError getDefaultMessage.

Prototype

@Override
    @Nullable
    public String getDefaultMessage() 

Source Link

Usage

From source file:com.baidu.stqa.signet.web.common.exception.ExceptionResolver.java

public Object getDecorateErrors(Exception ex) {
    HashMap<String, String> errors = new HashMap<String, String>();

    BindingResult bindingResult = ((MethodArgumentNotValidException) ex).getBindingResult();
    List<FieldError> fieldErrors = bindingResult.getFieldErrors();
    for (FieldError error : fieldErrors) {
        errors.put(error.getField(), error.getDefaultMessage());
    }// w  w w .ja va2s . c om
    return errors;
}

From source file:business.CustomResponseEntityExceptionHandler.java

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
        HttpHeaders headers, HttpStatus status, WebRequest request) {
    List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
    List<ObjectError> globalErrors = ex.getBindingResult().getGlobalErrors();
    List<String> errors = new ArrayList<>(fieldErrors.size() + globalErrors.size());
    for (FieldError fieldError : fieldErrors) {
        errors.add(fieldError.getField() + ", " + fieldError.getDefaultMessage());
    }//  w ww  .ja  va  2 s.c o  m
    for (ObjectError objectError : globalErrors) {
        errors.add(objectError.getObjectName() + ", " + objectError.getDefaultMessage());
    }
    ErrorMessage errorMessage = new ErrorMessage(errors);
    return new ResponseEntity<Object>(errorMessage, headers, status);
}

From source file:com.swcguild.capstoneproject.validation.RestValidationHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)

// #3: Specify the Http Status code to return when an error occurs
@ResponseStatus(HttpStatus.BAD_REQUEST)/*from  ww  w  .j  a v a 2 s. co m*/

// #4: Let Spring know that we have something to return in the body of the
// response. In this case it will be a ValidationErrorContainer containing
// a ValidationError object for each field that did not pass validation.
@ResponseBody
public ValidationErrorContainer processValidationErrors(MethodArgumentNotValidException e) {
    // #5: get the Binding Result and all field errors
    BindingResult result = e.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    // #6: Create a new ValidationError for each fieldError in the Binding Result
    ValidationErrorContainer errors = new ValidationErrorContainer();
    for (FieldError currentError : fieldErrors) {
        errors.addValidationError(currentError.getField(), currentError.getDefaultMessage());
    }
    return errors;
}

From source file:com.janrain.backplane.server.InvalidRequestException.java

public InvalidRequestException(BindingResult result) {

    for (FieldError error : result.getFieldErrors()) {
        fieldErrors.put(error.getField(), error.getDefaultMessage());
    }//from  w  w w . j a va 2s  .co  m
}

From source file:com.tsg.cms.validation.RESTValidationHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)//w w  w  .  j a  v  a 2  s .c om
@ResponseBody
public ValidationErrorContainer processMethodArgumentNotValidException(MethodArgumentNotValidException e) {

    BindingResult result = e.getBindingResult();
    List<FieldError> fieldErrors = result.getFieldErrors();

    ValidationErrorContainer errors = new ValidationErrorContainer();

    for (FieldError currentError : fieldErrors) {

        errors.addValidationError(currentError.getField(), currentError.getDefaultMessage());

    }

    return errors;

}

From source file:com.github.lynxdb.server.api.http.handlers.EpPut.java

@RequestMapping(path = "", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity put(Authentication _authentication, @RequestBody @Valid List<Metric> _request,
        BindingResult _bindingResult) {/* ww w .  j a va 2  s  . c o  m*/

    User user = (User) _authentication.getPrincipal();

    if (_bindingResult.hasErrors()) {
        ArrayList<String> errors = new ArrayList();
        _bindingResult.getFieldErrors().forEach((FieldError t) -> {
            errors.add(t.getField() + ": " + t.getDefaultMessage());
        });
        return new ErrorResponse(mapper, HttpStatus.BAD_REQUEST, errors.toString(), null).response();
    }

    List<com.github.lynxdb.server.core.Metric> metricList = new ArrayList<>();
    _request.stream().forEach((m) -> {
        metricList.add(new com.github.lynxdb.server.core.Metric(m));
    });

    try {
        entries.insertBulk(vhosts.byId(user.getVhost()), metricList);
    } catch (Exception ex) {
        throw ex;
    }
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
}

From source file:com.silentwu.schedule.dto.ResponseDto.java

@SuppressWarnings("unchecked")
public <T extends ResponseDto> T appendErrors(BindingResult result) {
    final List<FieldError> fieldErrors = result.getFieldErrors();
    for (FieldError fieldError : fieldErrors) {
        final String defaultMessage = fieldError.getDefaultMessage();
        this.message += defaultMessage + ";";
    }/*w  w w.  ja  va  2s  .  c o  m*/
    this.successful = false;
    return (T) this;
}

From source file:by.creepid.docgeneration.validation.ValidatorPhaseListener.java

private void validateValidatable(Validatable validatable, FacesContext facesContext) {
    Validator validator = (Validator) ContextHelper.getBean("firmRegValidator", Validator.class);

    WebUtils.clearFacesMessages(facesContext);

    Errors errors = new BindException(validatable, validatable.getClass().getSimpleName());
    validator.validate(validatable, errors);
    for (Object error : errors.getAllErrors()) {
        FieldError fieldError = (FieldError) error;

        String smessage = fieldError.getDefaultMessage();

        FacesMessage message = new FacesMessage();
        message.setSeverity(FacesMessage.SEVERITY_ERROR);
        message.setSummary(smessage);//from w w  w  . j  a  va 2s  .c  o  m
        message.setDetail(smessage);

        facesContext.addMessage(fieldError.getField(), message);
    }

    Iterator messages = facesContext.getMessages();
    while (messages.hasNext()) {
        FacesMessage message = (FacesMessage) messages.next();
        System.out.printf(message.getSummary() + "#" + message.getDetail());
    }

    if (errors.hasErrors()) {
        facesContext.renderResponse();
    }
}

From source file:com.garyclayburg.persistence.repository.CustomResponseEntityExceptionHandler.java

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
        HttpHeaders headers, HttpStatus status, WebRequest request) {
    List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
    List<ObjectError> globalErrors = ex.getBindingResult().getGlobalErrors();
    List<String> errors = new ArrayList<>(fieldErrors.size() + globalErrors.size());
    String error;// w w  w .j a v a  2s .  c o m
    for (FieldError fieldError : fieldErrors) {
        error = fieldError.getField() + ", " + fieldError.getDefaultMessage();
        errors.add(error);
    }
    for (ObjectError objectError : globalErrors) {
        error = objectError.getObjectName() + ", " + objectError.getDefaultMessage();
        errors.add(error);
    }
    ErrorMessage errorMessage = new ErrorMessage(errors);
    return new ResponseEntity(errorMessage, headers, status);
}

From source file:com.github.javarch.jsf.context.MessageContext.java

public void addError(BindingResult erros) {
    Assert.notNull(erros, "BindingResult no pode ser nulo.");

    for (FieldError fieldError : erros.getFieldErrors()) {
        addError(fieldError.getDefaultMessage());
    }/*from   w ww  .j  a v a 2 s  .c o  m*/
}