Allow HttpHeaders return values for @MVC methods

Allow HttpHeader instances to be returned directly from MVC controller
methods managed by HandlerMethodReturnValueHandler rather than needing
to be wrapped in a ResponseEntity.

Issue: SPR-11129
This commit is contained in:
Stephane Nicoll
2014-01-18 11:23:32 +01:00
committed by Rossen Stoyanchev
parent b92e4299b8
commit 7df25764af
3 changed files with 101 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
/*
* Copyright 2002-2014 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.servlet.mvc.method.annotation;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.util.Assert;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodReturnValueHandler;
import org.springframework.web.method.support.ModelAndViewContainer;
/**
* Handles {@link HttpHeaders} return values.
*
* @author Stephane Nicoll
* @since 4.0.1
*/
public class HttpHeadersReturnValueHandler implements HandlerMethodReturnValueHandler {
@Override
public boolean supportsReturnType(MethodParameter returnType) {
return HttpHeaders.class.isAssignableFrom(returnType.getParameterType());
}
@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
mavContainer.setRequestHandled(true);
Assert.isInstanceOf(HttpHeaders.class, returnValue);
HttpHeaders headers = (HttpHeaders) returnValue;
HttpServletResponse response = webRequest.getNativeResponse(HttpServletResponse.class);
ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(response);
if (!headers.isEmpty()) {
outputMessage.getHeaders().putAll(headers);
}
outputMessage.getBody(); // flush headers
}
}

View File

@@ -585,6 +585,7 @@ public class RequestMappingHandlerAdapter extends AbstractHandlerMethodAdapter i
handlers.add(new ModelMethodProcessor());
handlers.add(new ViewMethodReturnValueHandler());
handlers.add(new HttpEntityMethodProcessor(getMessageConverters(), this.contentNegotiationManager));
handlers.add(new HttpHeadersReturnValueHandler());
handlers.add(new CallableMethodReturnValueHandler());
handlers.add(new DeferredResultMethodReturnValueHandler());
handlers.add(new AsyncTaskMethodReturnValueHandler(this.beanFactory));