Use MessageSource to resolve @ResponseStatus.reason
The reason attribute of @ResponseStatus can now be a code resolvable through the ApplicationContext's MessageSource. Issue: SPR-6044
This commit is contained in:
@@ -1,13 +1,20 @@
|
||||
package org.springframework.web.servlet.mvc.annotation;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Locale;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.i18n.LocaleContextHolder;
|
||||
import org.springframework.context.support.StaticMessageSource;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.test.MockHttpServletResponse;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
/** @author Arjen Poutsma */
|
||||
@@ -46,6 +53,24 @@ public class ResponseStatusExceptionResolverTests {
|
||||
assertEquals("Invalid status reason", "You suck!", response.getErrorMessage());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void statusCodeAndReasonMessage() {
|
||||
Locale locale = Locale.CHINESE;
|
||||
LocaleContextHolder.setLocale(locale);
|
||||
try {
|
||||
StaticMessageSource messageSource = new StaticMessageSource();
|
||||
messageSource.addMessage("gone.reason", locale, "Gone reason message");
|
||||
exceptionResolver.setMessageSource(messageSource);
|
||||
|
||||
StatusCodeAndReasonMessageException ex = new StatusCodeAndReasonMessageException();
|
||||
ModelAndView mav = exceptionResolver.resolveException(request, response, null, ex);
|
||||
assertEquals("Invalid status reason", "Gone reason message", response.getErrorMessage());
|
||||
}
|
||||
finally {
|
||||
LocaleContextHolder.resetLocaleContext();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void notAnnotated() {
|
||||
Exception ex = new Exception();
|
||||
@@ -65,4 +90,11 @@ public class ResponseStatusExceptionResolverTests {
|
||||
private static class StatusCodeAndReasonException extends Exception {
|
||||
|
||||
}
|
||||
|
||||
@ResponseStatus(value = HttpStatus.GONE, reason = "gone.reason")
|
||||
@SuppressWarnings("serial")
|
||||
private static class StatusCodeAndReasonMessageException extends Exception {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user