{'code': 500, 'msg': 'Invalid JSON input: Cannot construct instance of `com.qianya.dto.api.ApiQueuesAddParm` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (\'{"inspectionNo": 79700, "name": "勔儘", "sex": 0, "age": 67, "type": 4, "checkItem": 0, "isAddProject": 0, "departmentsDataList": [{"departmentsId": 1, "departmentsProjectNumber": "1,2,3", "isBeforeDinner": 0}, {"departmentsId": 2, "departmentsProjectNumber": "4,5,6", "isBeforeDinner": 0}, {"departmentsId": 1, "departmentsProjectNumber": "7,8,9", "isBeforeDinner": 0}]}\'); nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.qianya.dto.api.ApiQueuesAddParm` (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value (\'{"inspectionNo": 79700, "name": "勔儘", "sex": 0, "age": 67, "type": 4, "checkItem": 0, "isAddProject": 0, "departmentsDataList": [{"departmentsId": 1, "departmentsProjectNumber": "1,2,3", "isBeforeDinner": 0}, {"departmentsId": 2, "departmentsProjectNumber": "4,5,6", "isBeforeDinner": 0}, {"departmentsId": 1, "departmentsProjectNumber": "7,8,9", "isBeforeDinner": 0}]}\')\n at [Source: (PushbackInputStream); line: 1, column: 1]', 'data': None}
时间: 2023-07-06 12:35:32 浏览: 131
这个错误提示是在进行 JSON 反序列化时出现的。它告诉我们,在将 JSON 字符串转换成对象时,发现了一个类 `com.qianya.dto.api.ApiQueuesAddParm` 没有提供一个能够接受字符串作为参数的构造函数,因此无法创建对象。
通常情况下,我们可以为这个类提供一个构造函数,接受一个字符串参数,然后在构造函数中解析字符串并初始化对象的属性。这样就可以成功地将 JSON 字符串转换成对象了。
在 Java 中,我们可以使用 Jackson 库来实现 JSON 反序列化。为了解决这个问题,我们需要在类中添加一个接受字符串参数的构造函数,例如:
```java
import com.fasterxml.jackson.databind.ObjectMapper;
public class ApiQueuesAddParm {
private int inspectionNo;
private String name;
private int sex;
private int age;
private int type;
private int checkItem;
private int isAddProject;
private List<DepartmentData> departmentsDataList;
public ApiQueuesAddParm() {
// 无参构造函数
}
public ApiQueuesAddParm(String jsonString) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
ApiQueuesAddParm obj = objectMapper.readValue(jsonString, ApiQueuesAddParm.class);
this.inspectionNo = obj.getInspectionNo();
this.name = obj.getName();
this.sex = obj.getSex();
this.age = obj.getAge();
this.type = obj.getType();
this.checkItem = obj.getCheckItem();
this.isAddProject = obj.getIsAddProject();
this.departmentsDataList = obj.getDepartmentsDataList();
}
// 省略 getter 和 setter 方法
}
```
这里我们添加了一个接受字符串参数的构造函数,并使用 Jackson 库将字符串反序列化成对象。注意,在这个构造函数中,我们需要将 Jackson 库添加到项目的依赖中,否则会出现编译错误。
使用这个构造函数的示例代码如下:
```java
String jsonString = "{\"inspectionNo\": 79700, \"name\": \"勔儘\", \"sex\": 0, \"age\": 67, \"type\": 4, \"checkItem\": 0, \"isAddProject\": 0, \"departmentsDataList\": [{\"departmentsId\": 1, \"departmentsProjectNumber\": \"1,2,3\", \"isBeforeDinner\": 0}, {\"departmentsId\": 2, \"departmentsProjectNumber\": \"4,5,6\", \"isBeforeDinner\": 0}, {\"departmentsId\": 1, \"departmentsProjectNumber\": \"7,8,9\", \"isBeforeDinner\": 0}]}";
ApiQueuesAddParm obj = new ApiQueuesAddParm(jsonString);
```
这样就可以解决这个问题了。
阅读全文