I have a spring controller method which takes a String
as the entirety of the RequestBody
like so:
@RequestMapping(method=RequestMethod.POST)
public @ResponseBody DTO method(@PathVariable("userId") long userId, @RequestBody String page) {
// Controller contents here
}
Previously, I have been able to post "naked" strings to this method, that is requests with a string as the body (and not a json object). For example (from chrome dev tools):
This stopped working after I attempted to add a Jackson Hibernate 4 module to the Jackson2ObjectMapperFactoryBean (to deal with hibernate lazy collections), using the following in my xml config file:
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper">
<bean class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="modulesToInstall"
value="com.fasterxml.jackson.datatype.hibernate4.Hibernate4Module"/>
<property name="featuresToEnable">
<array>
<util:constant static-field="com.fasterxml.jackson.core.JsonParser.Feature.ALLOW_SINGLE_QUOTES"/>
</array>
</property>
</bean>
</property>
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
After customising the ObjectMapper like this I receive the following error when posting the request as above:
org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Unrecognized token 'Testing': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@5e6a19fe; line: 1, column: 13]; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'Testing': was expecting ('true', 'false' or 'null')
at [Source: java.io.PushbackInputStream@5e6a19fe; line: 1, column: 13]
A work arround for this (using angular js) is to call JSON.stringify()
before posting to the API, so the angular code becomes
$http.post(Url + userId, JSON.stringify(string)).then(function (result) {
return result.data;
});
and the request becomes
After this the API works as it did previously, accepting the String on its own.
While I know that a string without enclosing "
is not a valid JSON string, what I don't understand is why changing the ObjectMapper bean should make such a difference in this case? I am using spring version 4.3.4 and jackson version 2.8.5, if that's at all useful information.