SPR-4518 - @RequestMapping methods returning null have an implicit ModelAndView created
This commit is contained in:
parent
c1667687d5
commit
6fbd198420
|
|
@ -25,12 +25,14 @@ import java.lang.annotation.Target;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Marks an exception class with the status code and reason that should be returned whenever said exception is thrown.
|
* Marks a method or exception class with the status code and reason that should be returned. The status code is applied
|
||||||
|
* to the HTTP response when the handler method is invoked, or whenever said exception is thrown.
|
||||||
*
|
*
|
||||||
* @author Arjen Poutsma
|
* @author Arjen Poutsma
|
||||||
|
* @see org.springframework.web.servlet.mvc.annotation.ResponseStatusExceptionResolver
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
@Target(ElementType.TYPE)
|
@Target({ElementType.TYPE, ElementType.METHOD})
|
||||||
@Retention(RetentionPolicy.RUNTIME)
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
@Documented
|
@Documented
|
||||||
public @interface ResponseStatus {
|
public @interface ResponseStatus {
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,7 @@ import org.springframework.web.bind.annotation.ModelAttribute;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.annotation.SessionAttributes;
|
import org.springframework.web.bind.annotation.SessionAttributes;
|
||||||
import org.springframework.web.bind.annotation.support.HandlerMethodInvoker;
|
import org.springframework.web.bind.annotation.support.HandlerMethodInvoker;
|
||||||
import org.springframework.web.bind.annotation.support.HandlerMethodResolver;
|
import org.springframework.web.bind.annotation.support.HandlerMethodResolver;
|
||||||
|
|
@ -672,6 +673,13 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
|
||||||
ExtendedModelMap implicitModel,
|
ExtendedModelMap implicitModel,
|
||||||
ServletWebRequest webRequest) {
|
ServletWebRequest webRequest) {
|
||||||
|
|
||||||
|
if (handlerMethod.isAnnotationPresent(ResponseStatus.class)) {
|
||||||
|
ResponseStatus responseStatus = handlerMethod.getAnnotation(ResponseStatus.class);
|
||||||
|
HttpServletResponse response = webRequest.getResponse();
|
||||||
|
response.setStatus(responseStatus.value().value());
|
||||||
|
responseArgumentUsed = true;
|
||||||
|
}
|
||||||
|
|
||||||
if (returnValue instanceof ModelAndView) {
|
if (returnValue instanceof ModelAndView) {
|
||||||
ModelAndView mav = (ModelAndView) returnValue;
|
ModelAndView mav = (ModelAndView) returnValue;
|
||||||
mav.getModelMap().mergeAttributes(implicitModel);
|
mav.getModelMap().mergeAttributes(implicitModel);
|
||||||
|
|
|
||||||
|
|
@ -54,6 +54,7 @@ import org.springframework.context.annotation.AnnotationConfigUtils;
|
||||||
import org.springframework.core.MethodParameter;
|
import org.springframework.core.MethodParameter;
|
||||||
import org.springframework.http.HttpInputMessage;
|
import org.springframework.http.HttpInputMessage;
|
||||||
import org.springframework.http.HttpOutputMessage;
|
import org.springframework.http.HttpOutputMessage;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
import org.springframework.http.converter.HttpMessageNotReadableException;
|
import org.springframework.http.converter.HttpMessageNotReadableException;
|
||||||
|
|
@ -78,6 +79,7 @@ import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
import org.springframework.web.bind.support.WebArgumentResolver;
|
import org.springframework.web.bind.support.WebArgumentResolver;
|
||||||
import org.springframework.web.bind.support.WebBindingInitializer;
|
import org.springframework.web.bind.support.WebBindingInitializer;
|
||||||
import org.springframework.web.context.WebApplicationContext;
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
|
@ -911,6 +913,17 @@ public class ServletAnnotationControllerTests {
|
||||||
assertEquals("text", response.getContentAsString());
|
assertEquals("text", response.getContentAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void responseStatus() throws ServletException, IOException {
|
||||||
|
initServlet(ResponseStatusController.class);
|
||||||
|
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/something");
|
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
|
servlet.service(request, response);
|
||||||
|
assertEquals("something", response.getContentAsString());
|
||||||
|
assertEquals(201, response.getStatus());
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Controllers
|
* Controllers
|
||||||
*/
|
*/
|
||||||
|
|
@ -1503,6 +1516,15 @@ public class ServletAnnotationControllerTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
public static class ResponseStatusController {
|
||||||
|
|
||||||
|
@RequestMapping("/something")
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
public void handle(Writer writer) throws IOException {
|
||||||
|
writer.write("something");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public static class MyMessageConverter implements HttpMessageConverter {
|
public static class MyMessageConverter implements HttpMessageConverter {
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue