Spring MVC:无法反序列化超出 START_ARRAY 令牌的实例 [英] Spring MVC: Can not deserialize instance of out of START_ARRAY token
问题描述
我已经为此挣扎了一段时间,但仍然没有快乐.我是 Spring 的新手,真的可以使用一些帮助.
我正在尝试使用 Spring Boot 从数据库表中返回代码列表.当我从浏览器中的 URL 调用我的 REST 控制器时...
示例网址: localhost:8081/cis/utl/lookupchoice/O.s/
它返回这个:
<预><代码>[{"lookupId":10,"choiceGroup":"O.s","choiceCode":null,"displayLabel":"Pending","hidden":false,"displayOrder":1},{"lookupId":11,"choiceGroup":"O.s","choiceCode":null,"displayLabel":"Active","hidden":false,"displayOrder":2},{"lookupId":12,"choiceGroup":"O.s","choiceCode":null,"displayLabel":"Archived","hidden":false,"displayOrder":3},{"lookupId":13,"choiceGroup":"O.s","choiceCode":null,"displayLabel":"Placeholder","hidden":false,"displayOrder":4}]但是,当我尝试从客户端控制器运行它时收到一条错误消息.调用如下所示:
<代码>////编译但不工作LookupChoice lookupChoice = restTemplate.getForObject(REST_URI+"/utl/lookupchoice/O.s/",LookupChoice.class);
错误: 嵌套异常为 com.fasterxml.jackson.databind.JsonMappingException:无法从 START_ARRAY 令牌中反序列化 com.MyPakage.repository.LookupChoice 的实例
假设错误是因为返回的是数组而不是单个对象,我将代码更改为:
<代码>////不编译IterablelookupChoice = restTemplate.getForObject(REST_URI+"/utl/lookupchoice/U.r/",Iterable );
但是,现在我在 Intellij 中遇到错误.它表示 Iterable<LookupChoice.class> 的预期表达式"strong> param,我无法克服这一点.
感谢您的时间和帮助,埃德
感谢您的回复.我选择了这个,现在似乎一切正常.感谢您的帮助!
RestTemplate restTemplate = new RestTemplate();ResponseEntity
它不编译的原因是因为在 Java 中不可能传递泛型类型参数的类,因为它们在运行时不存在.
这里有两个选项,使用数组 (LookupChoice[]
) 并在必要时将其转换为 List
:
restTemplate.getForObject(url, LookupChoice[]);
或者你可以使用ParameterizedTypeReference
:
restTemplate.exchange(url, HttpMethod.GET, null, new ParameterizedTypeReference>() {}).getBody()
虽然它是一个接口,所以要么继承它,要么使用匿名类,就像我在上面的代码示例中所做的那样.
另外,如果我没记错的话,ParameterizedTypeReference
只适用于 exchange()
方法,所以你会得到一个 ResponseEntity
而不是您的原始对象,因此您必须使用 getBody()
方法.
I have been beating my head against this for a while now and still no joy. I am new to Spring and could really use some help.
I am trying to use Spring Boot to return a list of codes from a DB table. When I call my REST controller from a URL in a browser...
Example URL: localhost:8081/cis/utl/lookupchoice/O.s/
It returns this:
[
{"lookupId":10,"choiceGroup":"O.s","choiceCode":null,"displayLabel":"Pending","hidden":false,"displayOrder":1},
{"lookupId":11,"choiceGroup":"O.s","choiceCode":null,"displayLabel":"Active","hidden":false,"displayOrder":2},
{"lookupId":12,"choiceGroup":"O.s","choiceCode":null,"displayLabel":"Archived","hidden":false,"displayOrder":3},
{"lookupId":13,"choiceGroup":"O.s","choiceCode":null,"displayLabel":"Placeholder","hidden":false,"displayOrder":4}
]
But, I get an error message when trying to run this from a client controller. The call looks like this:
//
//Compiles but does not work
LookupChoice lookupChoice = restTemplate.getForObject(REST_URI+"/utl/lookupchoice/O.s/",
LookupChoice.class);
The error: nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.MyPakage.repository.LookupChoice out of START_ARRAY token
Assuming the error occurs because an array is returned instead of a single object, I changed the code to this:
//
//Does not compile
Iterable<LookupChoice> lookupChoice = restTemplate.getForObject(REST_URI+"/utl/lookupchoice/U.r/",
Iterable<LookupChoice.class>);
But, now I get an error in Intellij. It's indicating that an "Expression expected" for the Iterable<LookupChoice.class> param, and I can't get past this.
Thank you for your time and assistance, Ed
Thanks for your reply. I opted for this and it all seems to work now. Thanks for your help!
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<Object[]> responseEntity;
Object[] lookupChoice;
responseEntity = restTemplate.getForEntity(REST_SERVICE_URI+"/utl/lookupchoice/O.s/" , Object[].class);
lookupChoice = responseEntity.getBody();
model.addAttribute("Status", lookupChoice);
The reason it doesn't compile is because it's impossible in Java to pass a class of generic type parameters because they don't exist at runtime.
You have two options here, either use an array (LookupChoice[]
) and convert it to a List<LookupChoice>
if necessary:
restTemplate.getForObject(url, LookupChoice[]);
Or you can use the ParameterizedTypeReference
:
restTemplate.exchange(url, HttpMethod.GET, null, new ParameterizedTypeReference<List<LookupChoice>>() {}).getBody()
It is an interface though, so either subclass it or use an anonymous class like I did in the code example above.
Also, ParameterizedTypeReference
only works on the exchange()
method if I'm not mistaken, so you'll get a ResponseEntity
in stead of your raw object, so you'll have to use the getBody()
method.
这篇关于Spring MVC:无法反序列化超出 START_ARRAY 令牌的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!