For example, I had to implement as below:
@RequestMapping(value = "/get-string", method = {RequestMethod.GET})
public @ResponseBody String getString() {
return "Hello World!";
}
When the action is called by Ajax in a JS file, the response received is: HelloWorld
. So, if the Ajax request is configured to only receive json encoded responses, I receive the standard deconding error. To solve this problem in server-side, I need to receive "HelloWorld"
.
My question is: There is a clean way I can do this, instead of just changing the string returned for the string below?
...
return "\"Hello World!\"";
}
UPDATE NOTE: In the time of this question, I was satisfied with the solution of returning a JSON map instead of a single string, as I wanted previously.
But now, I've spent some time searching about this, and trying to understand more about JSON patterns.
First of all, I found that my question is duplicated of these questions one, two and three. The correct answer says that the problem is with Spring Boot's default serializer (Jackson's library) that treats a string value (when serializing) as a raw JSON string, so it returns the value without double quotes instead of adding, as I was expecting.
And to be fair, I will choose the Bhargav's answer, that reached closer to what I was looking for. The answers from ddbullfrog and RohaNayak are correct, but they do not solve my problem correctly. Ademir Constantino's comment is correct too, he says to use plain/text instead of json format, using this reference. The Luay Absulraheem's answer is a correct way to config the action, but it doesn't work the way wanted, it continue to send the string as raw json. Thank you very much for your answers!
Additionally, it is possible to customize the Spring Boot to use Gson as its serializer, as can be seen on this article. After installing Gson dependency on your project, you just have to add this line on your application.properties file:
...
spring.http.converters.preferred-json-mapper=gson
In this way, you don't have to parse every time you want to send a string value to be serialized by Spring Boot and don't receive it without double quotes on your AJAX handler.
I was trying to achieve the same behavior I get on ASP.NET MVC, which used the JSON.NET library to perform serialization. This and the fact that AJAX accepts a single string (e.g. "hello world") as a JSON, have made me do a deeper research about JSON patterns.
There is more than one "standard" definition of a JSON format. As it's discussed on this Stack's other question, the current "INTERNET STANDARD" from JSON values is defined by RFC-8259 as it is said on page 5 of the document:
A JSON value MUST be an object, array, number, or string, or one of the following three literal names:
- false
- null
- true
As can be seen above, a JSON can be one wider range of data types than before in older standards.
UPDATE NOTE: Another alternative is to return a char[] instead of a String instance. In this way, Spring Boot will not recognize as raw JSON string:
@RequestMapping(value = "/get-string", method = {RequestMethod.GET})
public @ResponseBody char[] getString() {
return "Hello World!".toCharArray();
}