Example usage for org.springframework.web.bind MethodArgumentNotValidException getParameter

List of usage examples for org.springframework.web.bind MethodArgumentNotValidException getParameter

Introduction

In this page you can find the example usage for org.springframework.web.bind MethodArgumentNotValidException getParameter.

Prototype

public MethodParameter getParameter() 

Source Link

Document

Return the method parameter that failed validation.

Usage

From source file:org.jasig.ssp.transferobject.ServiceResponse.java

/**
 * Extract the Validation messages out of the
 * MethodArgumentNotValidException, and use as the ServiceResponse Message.
 * // w w w  . j  a  va  2  s.  co m
 * @param success
 *            If the response should indicate success or not
 * @param e
 *            Error messages to show
 */
public ServiceResponse(final boolean success, final MethodArgumentNotValidException e) {
    this.success = success;

    // collect the error messages
    final List<String> errorMessages = Lists.newArrayList();
    for (final ObjectError error : e.getBindingResult().getAllErrors()) {
        final StringBuilder sb = new StringBuilder(); // NOPMD

        // get the field name if it is a field error.
        if (error instanceof FieldError) {
            final FieldError fe = (FieldError) error;
            sb.append("[").append(fe.getField());
        } else {
            sb.append("[");
        }

        // get the default message
        sb.append(" ").append(error.getDefaultMessage()).append("] ");
        // add it to the list of error messages
        errorMessages.add(sb.toString());
    }

    // sort the messages for readablility
    Collections.sort(errorMessages);

    // introduce the error messages
    final int errorCount = e.getBindingResult().getErrorCount();
    final StringBuilder sb = new StringBuilder("Validation failed for argument ")
            .append(e.getParameter().getParameterName()).append(", with ").append(errorCount)
            .append(errorCount > 1 ? " errors: " : " error: ");

    // append the sorted error messages to the introduction and set as the
    // service response message.
    sb.append(StringUtils.join(errorMessages, ","));

    message = sb.toString();
}