MismatchedInputException:无法从VALUE_STRING令牌中反序列化java.util.ArrayList的实例

时间:2019-09-12 06:43:46

标签: java json spring spring-boot jackson

当有一个对象时,JSON字符串反序列化,但是当它具有对象列表时,反序列化失败

我正在使用Spring Boot RestTemplate调用一个返回JSON数组的外部URL。我需要在客户端将JSON数组反序列化为对象列表。每当我有一个JSON对象时,它都会反序列化而没有任何问题,但是当我在给定数组中有两个对象时,它将无法反序列化并抛出以下异常。

Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList` out of VALUE_STRING token

---在RestController内---

ResponseEntity <List<Student>> responseEntity = null;
List<Student> vehicle = null;

responseEntity = template.exchange(externalURL, HttpMethod.GET, request, new ParameterizedTypeReference<List<Student>>() { });

responseEntity.getBody();

---学生DTO ---

@JsonIgnoreProperties(ignoreUnknown = true)
public class Student {
private String name;
private String grade;
private Set<Address> addr = new HashSet<Address>();
private Set<Certification> cert = new HashSet<Certification>();

public Student() { }

@JsonCreator
public static List<Student> Create(String jsonValue) throws JsonParseException, JsonMappingException, IOException {

ObjectMapper mapper = new ObjectMapper();
List<Student> student = new ArrayList<Student>();
mapper.enable(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY);
student = mapper.readValue(jsonValue, new TypeReference<List<Student>>() {});
return student;
}

---地址DTO ---

public Address {
private String streetNo;
private String streetName;
}

---认证DTO ---

public Certification {
private String name;
private String certGrade;
}

--- JSON数组---

"[ {\n \"name\" : \"Tom\",
\n \"grade\" : \"Grade 5\",
\n \"addr\" : [ {
                \n \"streetNo\" : \"No 23\",
                \n \"streetName\" : \"Church road\"
                }, 
                {
                \n \"streetNo\" : \"No 43\",
                \n \"streetName\" : \"tumble road\"
                 } ],
\n \"cert\" : [ {
                \n \"name\" : \"cert in maths\",
                \n \"certGrade\" : \"first class\"\n 
                }, 
                {\n \"name\" : \"cert in language\",
                \n \"certGrade\" : \"second class\"\n 
                } ]\n}, 
{\n \"name\" : \"Marcus\",
\n \"grade\" : \"Grade 10\",
\n \"addr\" : [ {
                \n \"streetNo\" : \"No 21A\",
                \n \"streetName\" : \"Main street\"
                }, 
                {
                \n \"streetNo\" : \"No 22\",
                \n \"streetName\" : \"market street\"
                 } ],
\n \"cert\" : [ {
                \n \"name\" : \"cert in arts\",
                \n \"certGrade\" : \"first class\"\n 
                }, 
                {\n \"name\" : \"cert in music\",
                \n \"certGrade\" : \"second upper\"\n 
                } ]\n}]"

0 个答案:

没有答案