spring boot - 如何避免HTTP控制器处理程序中的“未能实例化[java.util.List]:指定的类是接口”?

2022-09-03 05:04:26

在我的 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()));

在这个API的泽西岛版本中,这工作得很好,但是使用Spring Boot,它给了我这个错误:

org.springframework.beans.BeanInstantiationException: Failed to instantiate [java.util.List]: Specified class is an interface

在Spring Boot中接受强类型列表的正确方法是什么?


答案 1

尝试将请求体注释添加到方法定义中

@RequestMapping(value="/custtable/update", method=RequestMethod.POST)
@ResponseBody
public String updateCusttableRecords(@RequestBody List<Custtable> customers) {
    //Method body 
}

答案 2

对我来说,我犯了一个错别字,不小心把一个类包装在一个列表中。删除拼写错误允许通过spring数据休息+jackson正确进行序列化。

List<MyClass> a; // typo
MyClass = a;// fix

推荐