使用fegin调用服务被调用的服务报No primary or single unique constructor found for interface java.util.List
时间: 2023-12-26 17:27:35 浏览: 45
根据提供的引用内容,出现"No primary or single unique constructor found for interface java.util.List"错误是因为Feign调用服务时,无法确定如何将请求参数转换为List类型的对象。为了解决这个问题,可以在后端接口的参数上添加@RequestBody注解,以明确指示Feign将请求体转换为List类型的对象。
以下是修改后的后端接口代码示例:
```java
@Operation(summary = "导出系统数据")
@PostMapping("/export")
public void export(@RequestBody List<SystemVO> systemVOList){
// 处理导出逻辑
uploadExportService.exportFileJson(systemVOList);
}
```
通过在参数前添加@RequestBody注解,Feign将能够正确地将请求体转换为List<SystemVO>类型的对象,从而解决"No primary or single unique constructor found for interface java.util.List"错误。
相关问题
get 请求No primary or single unique constructor found for interface java.util.List
根据提供的引用内容,报错"No primary or single unique constructor found for interface java.util.List"是由于在使用Spring MVC时,传递了一个List类型的参数,但是Spring无法找到List的主要构造函数或唯一构造函数。为了解决这个问题,可以使用两个注解来解决:@RequestParam和@ModelAttribute。
1. 使用@RequestParam注解:
```java
@RequestMapping(value = "/example", method = RequestMethod.GET)
public String exampleMethod(@RequestParam("listParam") List<String> listParam) {
// 处理请求
return "success";
}
```
在上述代码中,@RequestParam注解用于将请求参数与方法参数进行绑定。通过指定参数名,Spring将会自动将请求中的参数值转换为List类型的listParam参数。
2. 使用@ModelAttribute注解:
```java
@RequestMapping(value = "/example", method = RequestMethod.GET)
public String exampleMethod(@ModelAttribute("listParam") List<String> listParam) {
// 处理请求
return "success";
}
```
在上述代码中,@ModelAttribute注解用于将请求参数与方法参数进行绑定。通过指定参数名,Spring将会自动将请求中的参数值转换为List类型的listParam参数。
请注意,以上两种方法可以根据实际情况选择其中一种来解决问题。另外,确保在使用这些注解时,请求参数的名称与方法参数的名称相匹配。
No primary or single unique constructor found for interface java.util.List什么意思
"No primary or single unique constructor found for interface java.util.List"异常的意思是在使用某个接口的实现类时,没有找到主要的或唯一的构造函数。这通常是因为该接口是一个抽接口,不能直接实例化,需要使用其实现类来创建对象。在创建对象时,需要使用实现类的构造函数来初始化对象,而不是使用接口的构造函数。如果没有找到实现类的构造函数,则会出现该异常。