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

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

Introduction

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

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:br.com.modoagil.asr.rest.support.RESTErrorHandler.java

/**
 * Manipula exceo de validao de objetos nos servios
 *
 * @param ex/*w  w  w.j  a va  2  s  .c  o  m*/
 *            {@link MethodArgumentNotValidException}
 * @return resposta ao cliente
 */
@ResponseBody
@ExceptionHandler(MethodArgumentNotValidException.class)
public Response<E> processMethodArgumentNotValidException(final MethodArgumentNotValidException ex) {
    this.logger.info("handleMethodArgumentNotValidException - Catching: " + ex.getClass().getSimpleName(), ex);
    final BindingResult result = ex.getBindingResult();
    final FieldError fieldError = result.getFieldError();
    final String message = this.messageSource.getMessage(fieldError.getDefaultMessage(), null, null);
    return new ResponseBuilder<E>().success(false).status(HttpStatus.BAD_REQUEST).message(message).build();
}

From source file:com.netflix.genie.web.controllers.GenieExceptionMapperUnitTests.java

/**
 * Test method argument not valid exceptions.
 *
 * @throws IOException on error//from w  w w. j a va2 s  . co  m
 */
@Test
@SuppressFBWarnings(value = "DM_NEW_FOR_GETCLASS", justification = "It's needed for the test")
public void canHandleMethodArgumentNotValidExceptions() throws IOException {
    // Method is a final class so can't mock it. Just use the current method.
    final Method method = new Object() {
    }.getClass().getEnclosingMethod();
    final MethodParameter parameter = Mockito.mock(MethodParameter.class);
    Mockito.when(parameter.getMethod()).thenReturn(method);

    final BindingResult bindingResult = Mockito.mock(BindingResult.class);
    Mockito.when(bindingResult.getAllErrors()).thenReturn(Lists.newArrayList());

    final MethodArgumentNotValidException exception = new MethodArgumentNotValidException(parameter,
            bindingResult);

    this.mapper.handleMethodArgumentNotValidException(this.response, exception);
    Mockito.verify(this.response, Mockito.times(1))
            .sendError(Mockito.eq(HttpStatus.PRECONDITION_FAILED.value()), Mockito.anyString());
    Mockito.verify(counterId, Mockito.times(1)).withTag(MetricsConstants.TagKeys.EXCEPTION_CLASS,
            exception.getClass().getCanonicalName());
    Mockito.verify(counter, Mockito.times(1)).increment();
}