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.ns.retailmgr.controller.exception.RestControllerExceptionHandler.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;//from ww w.j a va 2 s  . 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);
    }
    RestErrorMessage RestErrorMessage = new RestErrorMessage(errors);
    return new ResponseEntity(RestErrorMessage, headers, status);
}

From source file:com.fengduo.bee.web.controller.BaseController.java

/**
 * ???/*from  ww w.j av a  2 s  . com*/
 * 
 * @param result
 * @return
 */
public String showFirstErrors(BindingResult result) {
    if (result.hasErrors()) {
        for (FieldError error : result.getFieldErrors()) {
            return error.getDefaultMessage();
        }
    }
    return StringUtils.EMPTY;
}

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

@RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity createVhost(Authentication _authentication, @RequestBody @Valid VhostCreationRequest _vcr,
        BindingResult _bindingResult) {//from   www .  j  av  a 2 s  .c o m

    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()).response();
    }

    Vhost v = new Vhost(_vcr);

    vhosts.save(v);

    return ResponseEntity.ok(v);
}

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

@RequestMapping(path = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE, consumes = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity post(@RequestBody @Valid SuggestRequest _request, Authentication _authentication,
        BindingResult _bindingResult) {//  w  w w  .j  a va2s .  com

    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();
    }

    return response(_request, _authentication);
}

From source file:th.co.geniustree.dental.controller.ValidationExceptionHandler.java

private Map<String, ValidationErrorMessage> extractError(MethodArgumentNotValidException ex) {
    Map<String, ValidationErrorMessage> returnError = new HashMap<>();
    List<ObjectError> allErrors = ex.getBindingResult().getAllErrors();

    for (ObjectError error : allErrors) {
        if (error instanceof FieldError) {
            FieldError fieldError = (FieldError) error;
            ValidationErrorMessage msg = new ValidationErrorMessage();
            msg.setMessage(fieldError.getDefaultMessage());
            msg.setType(fieldError.getObjectName());
            returnError.put(fieldError.getField(), msg);
        }/*www  . j  a  v a2s .  c  om*/
    }
    return returnError;
}

From source file:com.zq.fin.seckill.web.BaseController.java

 protected void validate(BindingResult result, Model model){
   ObjectError gloerror = result.getGlobalError();
   List<FieldError> errors= result.getFieldErrors();
      //from  w w  w.j  a  va 2 s .c o  m
   if(ObjectUtil.isNotEmpty(gloerror)){
      model.addAttribute("glo", gloerror.getDefaultMessage());
   }
   for(FieldError fieldError:errors){
      model.addAttribute(fieldError.getField(), fieldError.getDefaultMessage());
   }
}

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

@RequestMapping(value = "", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity createUser(Authentication _authentication, @RequestBody @Valid UserCreationRequest _ucr,
        BindingResult _bindingResult) {//from  w ww  .j  ava 2  s  .co  m

    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()).response();
    }

    User u = new User(_ucr);

    if (users.create(u)) {
        return ResponseEntity.ok(u);
    } else {
        return new ErrorResponse(mapper, HttpStatus.BAD_REQUEST, "User already exists").response();
    }
}

From source file:com.handu.open.dubbo.monitor.support.CommonResponse.java

/**
 * ???//from   w w w  .j a  va  2 s  . c o  m
 * @param message
 * @param errors
 */
public void fail(String message, List<FieldError> errors) {
    this.put("success", false);
    StringBuilder messageBuilder = new StringBuilder(message);
    messageBuilder.append("<ul>");
    for (FieldError fieldError : errors) {
        messageBuilder.append("<li>").append(fieldError.getDefaultMessage()).append("</li>");
    }
    messageBuilder.append("</ul>");
    this.put("message", messageBuilder.toString());
}

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

@RequestMapping(value = "/{userLogin}", method = RequestMethod.PUT, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity updateUser(Authentication _authentication, @PathVariable("userLogin") String userLogin,
        @RequestBody @Valid UserUpdateRequest _ucr, BindingResult _bindingResult) {

    if (_bindingResult.hasErrors()) {
        ArrayList<String> errors = new ArrayList();
        _bindingResult.getFieldErrors().forEach((FieldError t) -> {
            errors.add(t.getField() + ": " + t.getDefaultMessage());
        });/*from w  ww  .  j  a va 2s.  c  o m*/
        return new ErrorResponse(mapper, HttpStatus.BAD_REQUEST, errors.toString()).response();
    }

    User user = users.byLogin(userLogin);
    if (user == null) {
        return new ErrorResponse(mapper, HttpStatus.BAD_REQUEST, "User does not exist.", null).response();
    }

    if (_ucr.password != null && !_ucr.password.isEmpty()) {
        user.setPassword(_ucr.password);
    }
    if (_ucr.rank != null) {
        user.setRank(_ucr.rank);
    }

    users.save(user);

    return ResponseEntity.ok(user);
}

From source file:se.skltp.cooperation.web.rest.exception.DefaultExceptionHandler.java

@ExceptionHandler(MethodArgumentNotValidException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)//from   w w w .  j  a  va 2s . c om
@ResponseBody
public ProblemDetail processValidationError(HttpServletRequest request, MethodArgumentNotValidException e) {
    log.debug("Handling form validation error");

    ValidationError error = new ValidationError();
    buildErrorMessage(request, e, HttpStatus.BAD_REQUEST, error);

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

    for (FieldError fieldError : fieldErrors) {
        log.debug("Adding error message: {} to field: {}", fieldError.getDefaultMessage(),
                fieldError.getField());
        error.addFieldError(fieldError.getField(), fieldError.getCode(), fieldError.getDefaultMessage());
    }

    return error;
}