Support CharSequence for response body

Issue: SPR-13423
This commit is contained in:
Rossen Stoyanchev 2016-02-04 23:55:48 -05:00
parent 902a7287f7
commit 1291d6e1bd
3 changed files with 63 additions and 4 deletions

View File

@ -170,6 +170,13 @@ public abstract class AbstractMessageConverterMethodProcessor extends AbstractMe
Class<?> returnValueClass = getReturnValueType(returnValue, returnType);
Type returnValueType = getGenericType(returnType);
if (returnValue != null && returnValue instanceof CharSequence) {
returnValueClass = String.class;
returnValueType = String.class;
returnValue = (T) returnValue.toString();
}
HttpServletRequest servletRequest = inputMessage.getServletRequest();
List<MediaType> requestedMediaTypes = getAcceptableMediaTypes(servletRequest);
List<MediaType> producibleMediaTypes = getProducibleMediaTypes(servletRequest, returnValueClass, returnValueType);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@ -19,7 +19,6 @@ package org.springframework.web.servlet.mvc.method.annotation;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@ -31,7 +30,10 @@ import org.junit.Test;
import org.springframework.core.MethodParameter;
import org.springframework.http.HttpEntity;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.mock.web.test.MockHttpServletRequest;
import org.springframework.mock.web.test.MockHttpServletResponse;
@ -45,7 +47,10 @@ import org.springframework.web.context.request.ServletWebRequest;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.method.support.ModelAndViewContainer;
import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
/**
* Test fixture with {@link HttpEntityMethodProcessor} delegating to
@ -182,11 +187,35 @@ public class HttpEntityMethodProcessorTests {
assertTrue(content.contains("\"type\":\"bar\""));
}
// SPR-13423
@Test
public void handleReturnValueCharSequence() throws Exception {
List<HttpMessageConverter<?>>converters = new ArrayList<>();
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new StringHttpMessageConverter());
Method method = getClass().getDeclaredMethod("handle");
MethodParameter returnType = new MethodParameter(method, -1);
ResponseEntity<StringBuilder> returnValue = ResponseEntity.ok(new StringBuilder("Foo"));
HttpEntityMethodProcessor processor = new HttpEntityMethodProcessor(converters);
processor.handleReturnValue(returnValue, returnType, mavContainer, webRequest);
assertEquals("text/plain;charset=ISO-8859-1", servletResponse.getHeader("Content-Type"));
assertEquals("Foo", servletResponse.getContentAsString());
}
@SuppressWarnings("unused")
public void handle(HttpEntity<List<SimpleBean>> arg1, HttpEntity<SimpleBean> arg2) {
}
ResponseEntity<CharSequence> handle() {
return null;
}
@SuppressWarnings("unused")
private static abstract class MyParameterizedController<DTO extends Identifiable> {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@ -290,6 +290,24 @@ public class RequestResponseBodyMethodProcessorTests {
assertEquals("Foo", servletResponse.getContentAsString());
}
// SPR-13423
@Test
public void handleReturnValueCharSequence() throws Exception {
List<HttpMessageConverter<?>>converters = new ArrayList<>();
converters.add(new ByteArrayHttpMessageConverter());
converters.add(new StringHttpMessageConverter());
Method method = ResponseBodyController.class.getMethod("handleWithCharSequence");
MethodParameter returnType = new MethodParameter(method, -1);
RequestResponseBodyMethodProcessor processor = new RequestResponseBodyMethodProcessor(converters);
processor.handleReturnValue(new StringBuilder("Foo"), returnType, mavContainer, webRequest);
assertEquals("text/plain;charset=ISO-8859-1", servletResponse.getHeader("Content-Type"));
assertEquals("Foo", servletResponse.getContentAsString());
}
@Test
public void handleReturnValueStringAcceptCharset() throws Exception {
this.servletRequest.addHeader("Accept", "text/plain;charset=UTF-8");
@ -739,6 +757,11 @@ public class RequestResponseBodyMethodProcessorTests {
public String handle() {
return "hello";
}
@RequestMapping
public CharSequence handleWithCharSequence() {
return null;
}
}