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.
|
||||
@@ -22,6 +22,7 @@ import java.util.BitSet;
|
||||
import java.util.Collections;
|
||||
import java.util.Comparator;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
@@ -126,11 +127,28 @@ public class MimeType implements Comparable<MimeType>, Serializable {
|
||||
* Create a new {@code MimeType} for the given type, subtype, and character set.
|
||||
* @param type the primary type
|
||||
* @param subtype the subtype
|
||||
* @param charSet the character set
|
||||
* @param charset the character set
|
||||
* @throws IllegalArgumentException if any of the parameters contains illegal characters
|
||||
*/
|
||||
public MimeType(String type, String subtype, Charset charSet) {
|
||||
this(type, subtype, Collections.singletonMap(PARAM_CHARSET, charSet.name()));
|
||||
public MimeType(String type, String subtype, Charset charset) {
|
||||
this(type, subtype, Collections.singletonMap(PARAM_CHARSET, charset.name()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Copy-constructor that copies the type, subtype, parameters of the given {@code MimeType},
|
||||
* and allows to set the specified character set.
|
||||
* @param other the other media type
|
||||
* @param charset the character set
|
||||
* @throws IllegalArgumentException if any of the parameters contains illegal characters
|
||||
*/
|
||||
public MimeType(MimeType other, Charset charset) {
|
||||
this(other.getType(), other.getSubtype(), addCharsetParameter(charset, other.getParameters()));
|
||||
}
|
||||
|
||||
private static Map<String, String> addCharsetParameter(Charset charset, Map<String, String> parameters) {
|
||||
Map<String, String> map = new LinkedHashMap<String, String>(parameters);
|
||||
map.put(PARAM_CHARSET, charset.name());
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user