-
Notifications
You must be signed in to change notification settings - Fork 41.6k
Description
Setting gson as the preferred mapper via the spring.mvc.converters.preferred-json-mapper config property has the effect of the GsonHttpMessageConverter being inserted first in HttpMessageConverters, which has the consequence of giving it the highest priority; when Jackson is used, the other default converters such as ByteArrayHttpMessageConverter and StringHttpMessageConverter are still higher priority and thus have a chance to run.
Example:
@Controller
public class ExampleController {
@RequestMapping(value = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
public String test() {
return "{\"data\": \"example\"}";
}
}The above code, will work when Jackson is the default mapper, as StringHttpMessageConverter will handle the response and pass it through unescaped / unquoted. When gson is set as the preferred mapper, GsonHttpMessageConverter ends up double encoding the JSON, and you get a mangled response. The same problem arises if you have a method which attempts to return a StreamingResponseBody, etc.
In my opinion, simply switching JSON mappers should not have these other unintended side effects, and the behavior should not diverge this severely; ideally the Gson message converter would be inserted in the position that the Jackson converter currently sits in terms of priority, after StringHttpMessageConverter et al.