spring boot - 如何避免HTTP控制器处理程序中的“无法实例化[java.util.List]:指定的类是一个接口”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39427311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
spring boot - how to avoid "Failed to instantiate [java.util.List]: Specified class is an interface" in HTTP controller handler?
提问by Eugene Goldberg
In my spring boot REST API application, I need to handle HTTP POST by accepting a strongly-typed list as my input:
在我的 Spring Boot REST API 应用程序中,我需要通过接受一个强类型列表作为我的输入来处理 HTTP POST:
@RestController
public class CusttableController {
static final Logger LOG = LoggerFactory.getLogger(CusttableController.class);
@RequestMapping(value="/custtable/update", method=RequestMethod.POST)
@ResponseBody
public String updateCusttableRecords(List<Custtable> customers) {
try {
for (Custtable cust : customers) {
Custtable customer = (Custtable) custtableDao.getById(Custtable.class,
new CusttableCompositeKey
(cust.getAccountnum(),cust.getPartition(),cust.getDataareaid()));
In the Jersey version of this API, this worked just fine, but with Spring Boot, it gives me this error:
在这个 API 的 Jersey 版本中,这工作得很好,但是使用 Spring Boot,它给了我这个错误:
org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface
What is the proper way for me to accept a strongly-typed List in Spring Boot?
我在 Spring Boot 中接受强类型列表的正确方法是什么?
回答by Egor Lyashenko
Try to add RequestBodyannotation to your method definition
尝试将RequestBody注释添加到您的方法定义中
@RequestMapping(value="/custtable/update", method=RequestMethod.POST)
@ResponseBody
public String updateCusttableRecords(@RequestBody List<Custtable> customers) {
//Method body
}
回答by Miguel Pereira
For me I had made a typo accidentally wrapping a class in a list. Removing the typo allowed serialization to happen correctly via spring data rest + Hymanson.
对我来说,我不小心将一个类包装在一个列表中时犯了一个错字。删除错字允许通过 spring 数据休息 + Hymanson 正确进行序列化。
List<MyClass> a; // typo
MyClass = a;// fix