SPR-5766 - @ResponseBody

This commit is contained in:
Arjen Poutsma
2009-05-25 10:28:36 +00:00
parent af56f6497c
commit 6cb7f2cfc8
5 changed files with 86 additions and 6 deletions

View File

@@ -1,3 +1,19 @@
/*
* Copyright 2002-2009 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.bind.annotation;
import java.lang.annotation.Documented;
@@ -12,6 +28,7 @@ import java.lang.annotation.Target;
*
* @author Arjen Poutsma
* @see RequestHeader
* @see ResponseBody
* @see org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
* @since 3.0
*/

View File

@@ -0,0 +1,39 @@
/*
* Copyright 2002-2009 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.bind.annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation which indicates that a method return value should be bound to the web response body. Supported for annotated
* handler methods in Servlet environments.
*
* @author Arjen Poutsma
* @see RequestBody
* @see org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter
* @since 3.0
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ResponseBody {
}

View File

@@ -446,7 +446,14 @@ public class HandlerMethodInvoker {
Class paramType = methodParam.getParameterType();
MediaType contentType = inputMessage.getHeaders().getContentType();
if (contentType == null) {
throw new HttpMediaTypeNotSupportedException("Cannot extract @RequestBody: no Content-Type found");
StringBuilder builder = new StringBuilder(ClassUtils.getShortName(methodParam.getParameterType()));
String paramName = methodParam.getParameterName();
if (paramName != null) {
builder.append(' ');
builder.append(paramName);
}
throw new HttpMediaTypeNotSupportedException(
"Cannot extract @RequestBody parameter (" + builder.toString() + "): no Content-Type found");
}
List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
for (HttpMessageConverter<?> messageConverter : messageConverters) {

View File

@@ -50,12 +50,14 @@ import org.springframework.core.MethodParameter;
import org.springframework.core.ParameterNameDiscoverer;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.http.HttpInputMessage;
import org.springframework.http.HttpOutputMessage;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.xml.SourceHttpMessageConverter;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.http.server.ServletServerHttpResponse;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
import org.springframework.util.AntPathMatcher;
@@ -76,6 +78,7 @@ import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.bind.annotation.support.HandlerMethodInvoker;
@@ -697,7 +700,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
Class handlerType,
Object returnValue,
ExtendedModelMap implicitModel,
ServletWebRequest webRequest) {
ServletWebRequest webRequest) throws Exception {
if (handlerMethod.isAnnotationPresent(ResponseStatus.class)) {
ResponseStatus responseStatus = handlerMethod.getAnnotation(ResponseStatus.class);
@@ -717,6 +720,18 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
}
}
if (returnValue != null && handlerMethod.isAnnotationPresent(ResponseBody.class)) {
Class returnValueType = returnValue.getClass();
HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());
for (HttpMessageConverter messageConverter : messageConverters) {
if (messageConverter.supports(returnValueType)) {
messageConverter.write(returnValue, outputMessage);
responseArgumentUsed = true;
return null;
}
}
}
if (returnValue instanceof ModelAndView) {
ModelAndView mav = (ModelAndView) returnValue;
mav.getModelMap().mergeAttributes(implicitModel);