【发布时间】:2022-12-27 15:59:07
【问题描述】:
Use case: Return a list of JSON Strings from Spring Rest Controller (the JSON strings come from a third party library).
Problem: Response from REST Controller has escape characters. This happens only when the return type is List or array or any other collection type. Returning a single string works fine.
How to return list of JSON formatted strings but avoid the escape characters.
Code:
import java.util.Arrays;
import java.util.List;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("restjson")
public class RestJsonController {
@GetMapping(value="list", produces = {MediaType.APPLICATION_JSON_VALUE})
public List<String> getValues(){
String value1 = "{\"name\":\"John\", \"age\":30}";
String value2 = "{\"name\":\"Tom\", \"age\":21}";
return Arrays.asList(value1, value2);
//response has escape characters:
//["{\"name\":\"John\", \"age\":30}","{\"name\":\"Tom\", \"age\":21}"]
}
@GetMapping(value="single", produces = {MediaType.APPLICATION_JSON_VALUE})
public String getValue(){
String value1 = "{\"name\":\"John\", \"age\":30}";
String value2 = "{\"name\":\"Tom\", \"age\":21}";
return value1.concat(value2);
//response has no escape characters:
//{"name":"John", "age":30}{"name":"Tom", "age":21}
}
}
Springboot version: 2.7.0
Full code at: https://github.com/rai-sandeep/restjson/blob/main/src/main/java/com/sdprai/restjson/controller/RestJsonController.java
EDIT:
To avoid any confusion related to string concatenation, I have updated the code (see below). Returning a list even with just one JSON string results in escape characters in the response. But returning just a string does not have this problem. I don't understand the reason behind this difference. For my use case, is there a way to return a list of JSON strings without the escape characters?
import java.util.Collections;
import java.util.List;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("restjson")
public class RestJsonController {
@GetMapping(value="list", produces = {MediaType.APPLICATION_JSON_VALUE})
public List<String> getValues(){
String value1 = "{\"name\":\"John\", \"age\":30}";
return Collections.singletonList(value1);
//returns: ["{\"name\":\"John\", \"age\":30}"]
}
@GetMapping(value="single", produces = {MediaType.APPLICATION_JSON_VALUE})
public String getValue(){
String value1 = "{\"name\":\"John\", \"age\":30}";
return value1;
//returns: {"name":"John", "age":30}
}
}
【问题讨论】:
-
return value1.concat(value2);- does not produce json. -
Agreed, but that was just a hack I tried to get around the problem. To make it a valid json, I have to format it as a json array. And see edit, I used a single string to avoid confusions related to concatenation.
-
I'm having the same issue where I need to return the
List<String>inResponseEntitywhere this String contains a single JSON String. And while sending the response viaResponseEntityserializes the whole List object as well try to serialize the single value ofListwhich is json. Do you found any solution for this? -
@Pash0002 No, I haven't found a good solution. As a workaround, I'm returning a single json string from the list, but it's not ideal.
return StringUtils.join("[", list.stream().collect(Collectors.joining(",")), "]"); -
@Sandeep Rai You can serialize using the
ObjectMapper(Jackson library). But for that you need to change return type toString. This worked for me..
标签: java spring spring-boot spring-restcontroller