I have a Spring MVC controller with the following method:
@RequestMapping(value = "/stringtest", method = RequestMethod.GET)
public String simpletest() throws Exception {
return "test";
}
This sits inside a controller that starts like this:
@RestController
@RequestMapping(value = "/root")
public class RootController
When I call other methods that return objects, those objects are serialized by Jackson into JSON. But this method which returns a String is not converted to JSON. In case it's not clear, here's an example using curl:
$curl http://localhost:8080/clapi/root/stringtest
test
So the problem is that "test" without any quotes is not a JSON string, but my REST client is expecting a string. I expected the curl command to show that string with quotes around it so it's legal JSON instead:
"test"
I am using Spring WebMVC 4.1.3 and Jackson 2.4.3. I have tried adding a "produces" attribute to the RequestMapping to say it should return JSON. In this case, the Content-Type attribute sent back is "application/json" but still the test string is not quoted.
I could workaround this by calling a JSON library to convert by Java String to JSON, but it seems like Spring MVC and Jackson generally do this automatically. Yet somehow they are not doing it in my case. Any ideas what I might have configured wrong to be getting just test back instead of "test"?
It turns out that when you use the @EnableWebMvc
annotation that it turns on a bunch of http message converters by default. The second one in the list is the StringHttpMessageConverter
which the documentation says will be applied for text/*
content types. However, after stepping through with the debugger, it applies to String objects for */*
content types- which obviously includes application/json
.
The MappingJackson2HttpMessageConverter
which is responsible for application/json
content types is further down on this list. So for Java objects other than String, this one gets called. That's why it was working for Object and Array types, but not String- despite the good suggestions of using the produces attribute to set application/json
content type. Although that content type is necessary to trigger this converter, the String converter was grabbing the job first!
As I was extending the WebMvcConfigurationSupport
class for some other configuration, I overrode the following method to put the Jackson converter first so when the content-type is application/json
then this one will be used instead of the String converter:
@Override
protected void configureMessageConverters(
List<HttpMessageConverter<?>> converters) {
// put the jackson converter to the front of the list so that application/json content-type strings will be treated as JSON
converters.add(new MappingJackson2HttpMessageConverter());
// and probably needs a string converter too for text/plain content-type strings to be properly handled
converters.add(new StringHttpMessageConverter());
}
Now when I call the test method from curl I get the desired "test"
output instead of just test
, so the angular client which is expecting JSON is now happy.
Try this:
@RequestMapping(value = "/stringtest", method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With