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

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

Introduction

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

Prototype

public MethodArgumentNotValidException(MethodParameter parameter, BindingResult bindingResult) 

Source Link

Document

Constructor for MethodArgumentNotValidException .

Usage

From source file:fi.helsinki.opintoni.exception.ExceptionThrower.java

@RequestMapping("/validationerror")
public void throwValidationError() throws MethodArgumentNotValidException {
    MethodParameter methodParameter = mock(MethodParameter.class);
    BindingResult bindingResult = mock(BindingResult.class);
    throw new MethodArgumentNotValidException(methodParameter, bindingResult);
}

From source file:com.kixeye.chassis.transport.websocket.RawWebSocketMessage.java

/**
 * Deserializes the given message./*  w w  w .j a  va2s .com*/
 * 
 * @param action
 * @return
 * @throws Exception
 */
public T deserialize(WebSocketAction action) throws Exception {
    // first deserialize
    T message = null;

    if (messageClass != null) {
        message = serDe.deserialize(new ByteBufferBackedInputStream(rawData), messageClass);
    }

    // then validate
    if (message != null && action.shouldValidatePayload()) {
        SpringValidatorAdapter validatorAdapter = new SpringValidatorAdapter(messageValidator);

        BeanPropertyBindingResult result = new BeanPropertyBindingResult(message, messageClass.getName());

        validatorAdapter.validate(message, result);

        if (result.hasErrors()) {
            throw new MethodArgumentNotValidException(
                    new MethodParameter(action.getMethod(), action.getPayloadParameterIndex()), result);
        }
    }

    return message;
}

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

/**
 * Test method argument not valid exceptions.
 *
 * @throws IOException on error/*from  ww w  . ja v a  2 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();
}

From source file:com.monarchapis.driver.spring.rest.ApiErrorResponseEntityExceptionHandlerTest.java

@Test
public void testMethodArgumentNotValidException() {
    performTest(//
            new MethodArgumentNotValidException(null, null), //
            400, //
            "invalidParameter");
}