Search code examples
javaspringrestspring-mvcspring-validator

Is there a way to send only Error Code and not Error Field in Spring Validator?


We have a RestFul webservice in spring and for validating the request parameter we use Spring validator IThe following is how my validator looks :

 @Override
public void validate( Object oObject, Errors oErrors ) 
{

    RequestDAO oRequest = (RequestDAO) oObject;

    if (Validation on FIELD 1 fails  ) 
    {
       oErrors.rejectValue( "FIELD1", ERROR CODE );
    } 
  else if ( If any other validation fails ) 
 {
        oErrors.rejectValue( "", A Different error code );
        //I just want to send the ERROR CODE here and not the name of the filed
        //IS it the correct way to do it?
    }

}

In my Exception handler class I get the validation error and generate my custom Exception:

  protected AppException convertValidionErrorToCustomException(    MethodArgumentNotValidException oMethodArgNotvalidExp ) {

    CustomException oAppExp = null;

    BindingResult oBindingResult = oMethodArgNotvalidExp.getBindingResult();
    // Get the first error associated with a field, if any.
    FieldError oFieldError = oBindingResult.getFieldError();
    // IF I DONT SENT THE NAME OF THE FIELD I GET NULL ABOVE and thus an NP exp
    String sCode = oFieldError.getCode();
    if ( StringUtils.isEmpty( sCode ) || !StringUtils.isNumeric( sCode ) )
  {

        oAppExp = new CustomException( oMethodArgNotvalidExp );

    } 
  else 
  {

        int iErrorCode = Integer.parseInt( oFieldError.getCode() );
        String sFieldName = oFieldError.getField();
        if ( !StringUtils.isEmpty( sFieldName ) ) {

            oAppExp = new CustomException( iErrorCode, sFieldName );

        } else {

            oAppExp = new CustomException( iErrorCode );
        }
    }

    return oAppExp;
}

My question is : How can i send only error Code and not Filed Name from Validator class. Also How to modify my Exception handler method to handle a the following 2 scenario:

  • When Field name and error code are sent

  • When only error code is sent and not filed name.


Solution

  • I Finally found a way. I updated my exception handler to have this :

       protected CustomException convertValidionErrorToAppException( MethodArgumentNotValidException oMethodArgNotvalidExp ) {
    
        CustomException Exp = null;
    
        BindingResult oBindingResult = oMethodArgNotvalidExp.getBindingResult();
        // Get the first error associated with a field, if any.\
        if ( oBindingResult.hasFieldErrors() ) {
            Exp = processFieldError( oBindingResult, oMethodArgNotvalidExp );
    
        } else {
            Exp = processErrors( oBindingResult, oMethodArgNotvalidExp );
        }
    
        return Exp;
    }
    

    The processFieldError( oBindingResult, oMethodArgNotvalidExp ); method processes all field errors and the processErrors( oBindingResult, oMethodArgNotvalidExp ) will process non-field errors. The method looks like this

       protected CustomExceptionprocessErrors( BindingResult oBindingResult,     MethodArgumentNotValidException oMethodArgNotvalidExp ) {
        CustomException Exp = null;
        //Get all errors from Binding Errors
        ObjectError oError = oBindingResult.getAllErrors().get( 0 );
        String sCode = oError.getCode();
        if ( StringUtils.isEmpty( sCode ) || !StringUtils.isNumeric( sCode ) ) {
    
            Exp = new CustomException( oMethodArgNotvalidExp );
    
        } else {
            int iErrorCode = Integer.parseInt( sCode );
            Exp = new CustomException( iErrorCode );
        }
        return Exp;
    }