Skip to content

SPR-13183 Send BindingResult Errors in ResponseEntityExceptionHandler #831

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.http.HttpHeaders;
Expand All @@ -33,6 +32,7 @@
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindException;
import org.springframework.validation.BindingResult;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
Expand Down Expand Up @@ -394,7 +394,12 @@ protected ResponseEntity<Object> handleHttpMessageNotWritable(HttpMessageNotWrit
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
HttpHeaders headers, HttpStatus status, WebRequest request) {

return handleExceptionInternal(ex, null, headers, status, request);
Object body = null;
BindingResult bindingResult = ex.getBindingResult();
if (bindingResult !=null) {
body = bindingResult.getAllErrors();
}
return handleExceptionInternal(ex, body, headers, status, request);
}

/**
Expand Down Expand Up @@ -424,7 +429,7 @@ protected ResponseEntity<Object> handleMissingServletRequestPart(MissingServletR
protected ResponseEntity<Object> handleBindException(BindException ex, HttpHeaders headers,
HttpStatus status, WebRequest request) {

return handleExceptionInternal(ex, null, headers, status, request);
return handleExceptionInternal(ex, ex.getAllErrors(), headers, status, request);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,11 +19,11 @@
import java.util.Arrays;
import java.util.EnumSet;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.junit.Before;
import org.junit.Test;

import org.springframework.beans.ConversionNotSupportedException;
import org.springframework.beans.TypeMismatchException;
import org.springframework.core.MethodParameter;
Expand All @@ -37,6 +37,8 @@
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
import org.springframework.tests.sample.beans.TestBean;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotAcceptableException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
Expand All @@ -56,7 +58,6 @@
import org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver;

import static org.junit.Assert.*;
import org.mockito.Mockito;

/**
* Test fixture for {@link ResponseEntityExceptionHandler}.
Expand Down Expand Up @@ -179,9 +180,16 @@ public void httpMessageNotWritable() {
}

@Test
public void methodArgumentNotValid() {
Exception ex = Mockito.mock(MethodArgumentNotValidException.class);
testException(ex);
public void methodArgumentNotValid() throws Exception {
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(new TestBean(), "testBean");
errors.rejectValue("name", "invalid");
MethodParameter parameter = new MethodParameter(this.getClass().getMethod("handle", String.class), 0);
MethodArgumentNotValidException ex = new MethodArgumentNotValidException(parameter, errors);
ResponseEntity<Object> entity = testException(ex);
Object body = entity.getBody();
assertNotNull("Empty entity body", body);
assertEquals("[Field error in object 'testBean' on field 'name': rejected value [null]; codes [invalid.testBean.name,invalid.name,invalid.java.lang.String,invalid]; arguments []; default message [null]]",
body.toString());
}

@Test
Expand All @@ -192,8 +200,14 @@ public void missingServletRequestPart() {

@Test
public void bindException() {
Exception ex = new BindException(new Object(), "name");
testException(ex);
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(new TestBean(), "testBean");
errors.rejectValue("name", "invalid");
Exception ex = new BindException(errors);
ResponseEntity<Object> entity = testException(ex);
Object body = entity.getBody();
assertNotNull("Empty entity body", body);
assertEquals("[Field error in object 'testBean' on field 'name': rejected value [null]; codes [invalid.testBean.name,invalid.name,invalid.java.lang.String,invalid]; arguments []; default message [null]]",
body.toString());
}

@Test
Expand Down Expand Up @@ -255,8 +269,7 @@ protected ResponseEntity<Object> handleServletRequestBindingException(ServletReq
}
}

@SuppressWarnings("unused")
void handle(String arg) {
public void handle(String arg) {
}

}