I want to pass a JSON to the client on request. My actual code looks something like this:
...
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;
...
@RestController
public class Controller {
@Value("classpath:file.json")
private Resource jsonFile;
@GetMapping(path ="json", produces = "application/json")
public String getJSON() {
try (InputStream in = jsonFile.getInputStream()) {
return IOUtils.toString(in, StandardCharsets.UTF_8.name());
}
catch ...
}
}
...
jsonFile is the .json file whose content i want to send back to the user, the @Value annotation adds the classpath.
The Issue is: the Response is not only the json, but also linebreaks are encodede as \n
and the entire JSON is encapsulated in quotation marks making it one big string instead of an object. As the JSON is actually a .json file i would not like to parse it into an object first.
jsonFile looks like:
{
"thing1": [
{
"list1": [
"abc"
],
"subthing1": {
"list1": [
"aaa",
"bbb"
],
"list2": [
"ccc"
],
"list3": [
"zzz"
],
},
"subthing2":
...
Edit 1: Returning JSON object as response in Spring Boot 's answers last paragraph states "If you want to return a json serialized string then just return the string.", which red for me: "if you have a json in a string, return the string"