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"?
["test"]
. So I expect if I return a simple string I should get"test"
– Jesse Pangburn"test"
as output so both the Javascript web implementations and Angular believe that a quoted string is valid JSON. Still, would be nice if the browsers and the RFC agreed with each other! – Jesse Pangburn