Allow to specify AbstractHttpMessageConverter default charset
Before this commit, specifying the charset to use with produces or consumes @RequestMapping attributes resulted in default charset loss. That was really annoying for JSON for example, where using UTF-8 charset is mandatory in a lot of use cases. This commit adds a defaultCharset property to AbstractHttpMessageConverter in order to avoid losing the default charset when specifying the charset with these @RequestMapping attributes. It changes slightly the default behavior (that's why we have waited 4.3), but it is much more error prone, and will match with most user's expectations since the charset loss was accidental in most use cases (users usually just want to limit the media type supported by a specific handler method). Issue: SPR-13631
This commit is contained in:
@@ -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.
|
||||
@@ -67,7 +67,7 @@ public class AsyncTests {
|
||||
|
||||
this.mockMvc.perform(asyncDispatch(mvcResult))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
|
||||
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ public class AsyncTests {
|
||||
|
||||
this.mockMvc.perform(asyncDispatch(mvcResult))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
|
||||
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
|
||||
}
|
||||
|
||||
@@ -94,7 +94,7 @@ public class AsyncTests {
|
||||
|
||||
this.mockMvc.perform(asyncDispatch(mvcResult))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
|
||||
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
|
||||
}
|
||||
|
||||
@@ -122,7 +122,7 @@ public class AsyncTests {
|
||||
|
||||
this.mockMvc.perform(asyncDispatch(mvcResult))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
|
||||
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ public class AsyncTests {
|
||||
|
||||
this.mockMvc.perform(asyncDispatch(mvcResult))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
|
||||
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
|
||||
}
|
||||
|
||||
@@ -161,7 +161,7 @@ public class AsyncTests {
|
||||
this.mockMvc.perform(asyncDispatch(mvcResult))
|
||||
.andDo(print(writer))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
|
||||
.andExpect(content().string("{\"name\":\"Joe\",\"someDouble\":0.0,\"someBoolean\":false}"));
|
||||
|
||||
assertTrue(writer.toString().contains("Async started = false"));
|
||||
|
||||
@@ -16,8 +16,6 @@
|
||||
|
||||
package org.springframework.test.web.servlet.samples.standalone.resultmatchers;
|
||||
|
||||
import java.nio.charset.Charset;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -44,8 +42,6 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*;
|
||||
*/
|
||||
public class ContentAssertionTests {
|
||||
|
||||
public static final MediaType TEXT_PLAIN_UTF8 = new MediaType("text", "plain", Charset.forName("UTF-8"));
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
@@ -56,8 +52,10 @@ public class ContentAssertionTests {
|
||||
@Test
|
||||
public void testContentType() throws Exception {
|
||||
this.mockMvc.perform(get("/handle").accept(MediaType.TEXT_PLAIN))
|
||||
.andExpect(content().contentType(MediaType.TEXT_PLAIN))
|
||||
.andExpect(content().contentType("text/plain"));
|
||||
.andExpect(content().contentType(MediaType.valueOf("text/plain;charset=ISO-8859-1")))
|
||||
.andExpect(content().contentType("text/plain;charset=ISO-8859-1"))
|
||||
.andExpect(content().contentTypeCompatibleWith("text/plain"))
|
||||
.andExpect(content().contentTypeCompatibleWith(MediaType.TEXT_PLAIN));
|
||||
|
||||
this.mockMvc.perform(get("/handleUtf8"))
|
||||
.andExpect(content().contentType(MediaType.valueOf("text/plain;charset=UTF-8")))
|
||||
|
||||
@@ -158,7 +158,7 @@ public class XpathAssertionTests {
|
||||
standaloneSetup(new BlogFeedController()).build()
|
||||
.perform(get("/blog.atom").accept(MediaType.APPLICATION_ATOM_XML))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(MediaType.APPLICATION_ATOM_XML))
|
||||
.andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_ATOM_XML))
|
||||
.andExpect(xpath("//feed/title").string("Test Feed"))
|
||||
.andExpect(xpath("//feed/icon").string("http://www.example.com/favicon.ico"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user