英文:
JSON parse error: Cannot deserialize instance of ArrayList
问题
以下是翻译好的部分:
我正在尝试将 JSON 发送到一个 POST bodyRequest 服务,以便将此信息用作 Java ArrayList 或类似的数据结构。当我尝试执行此操作时,我收到了一个反序列化错误。
这是我的 JSON:
{information: [ {fields: "Periods Offered", tables: "Courses",
columns: "Academic Level*"}, {fields: "Default Grading Basis*",
tables: "Courses", columns: "Default Offering Percentage"}, {fields:
"Allowed Locations", tables: "Courses", columns: "Allowed to Offer"}
] }
这是我的 POST bodyRequest 服务:
@ResponseBody
@PostMapping("/dataMapping/update/table")
public ResponseEntity<Object> updateDataMappingTable(@RequestBody List<UpdateDataMapping> information) {
try {
String update = this.dataMappingService.update(information);
return new ResponseEntity<Object>(update, HttpStatus.OK);
} catch (Exception e) {
Response response = new Response();
response.setError(e.getMessage());
return new ResponseEntity<Object>(response, HttpStatus.OK);
}
}
我的 UpdateDataMapping 类如下所示:
public class UpdateDataMapping {
public String fields;
public String tables;
public String columns;
public UpdateDataMapping() {
}
public UpdateDataMapping(String fields, String tables, String columns) {
this.fields = fields;
this.tables = tables;
this.columns = columns;
}
public String getFields() {
return fields;
}
public void setFields(String fields) {
this.fields = fields;
}
public String getTables() {
return tables;
}
public void setTables(String tables) {
this.tables = tables;
}
public String getColumns() {
return columns;
}
public void setColumns(String columns) {
this.columns = columns;
}
}
这是我的错误信息:
.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.util.ArrayList<com.kastech.model.UpdateDataMapping>` out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.util.ArrayList<com.kastech.model.UpdateDataMapping>` out of START_OBJECT token at [Source: (PushbackInputStream); line: 1, column: 1]]
如果有人能帮助我,我将非常感谢。
英文:
I'm trying to send a JSON to a POST bodyRequest service to use this information as a Java ArrayList or something like that. When I try to do it, I receive a deserialize error
This is my JSON:
> {information: [ {fields: "Periods Offered", tables: "Courses",
> columns: "Academic Level*"}, {fields: "Default Grading Basis*",
> tables: "Courses", columns: "Default Offering Percentage"}, {fields:
> "Allowed Locations", tables: "Courses", columns: "Allowed to Offer"}
> ] }
This is my POST bodyRequest service
@ResponseBody
@PostMapping("/dataMapping/update/table")
public ResponseEntity<Object> updateDataMappingTable(@RequestBody List<UpdateDataMapping> information) {
try {
String update = this.dataMappingService.update(information);
return new ResponseEntity<Object>(update, HttpStatus.OK);
} catch (Exception e) {
Response response = new Response();
response.setError(e.getMessage());
return new ResponseEntity<Object>(response, HttpStatus.OK);
}
}
My UpdateDataMapping class looks like:
public class UpdateDataMapping {
public String fields;
public String tables;
public String columns;
public UpdateDataMapping() {
}
public UpdateDataMapping(String fields, String tables, String columns) {
this.fields = fields;
this.tables = tables;
this.columns = columns;
}
public String getFields() {
return fields;
}
public void setFields(String fields) {
this.fields = fields;
}
public String getTables() {
return tables;
}
public void setTables(String tables) {
this.tables = tables;
}
public String getColumns() {
return columns;
}
public void setColumns(String columns) {
this.columns = columns;
}
}
This is my error:
>.w.s.m.s.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of java.util.ArrayList<com.kastech.model.UpdateDataMapping>
out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.ArrayList<com.kastech.model.UpdateDataMapping>
out of START_OBJECT token
at [Source: (PushbackInputStream); line: 1, column: 1]]
If someone can help me I will appreciate it.
答案1
得分: 1
你的JSON示例显示,你的数组位于一个名为 information
的对象中。你之所以看到当前的堆栈跟踪,是因为Jackson无法找到合适的方法来反序列化你的请求数据。
要么,你可以将你的请求JSON更改为一个简单的列表
[ {fields: "Periods Offered", tables: "Courses", columns: "Academic Level*"}, {fields: "Default Grading Basis*", tables: "Courses", columns: "Default Offering Percentage"}, {fields: "Allowed Locations", tables: "Courses", columns: "Allowed to Offer"} ]
要么你可以有一个POJO来表示那个 information
对象
class UpdateMappingList {
List<UpdateDataMapping> information;
...
}
英文:
Your JSON example shows that your array is delivered inside an information
object. You are seeing your current stack trace because the Jackson is unable to find an appropriate way to deserialize your request payload.
Either, you can change your request JSON to simply be a list
[ {fields: "Periods Offered", tables: "Courses", columns: "Academic Level*"}, {fields: "Default Grading Basis*", tables: "Courses", columns: "Default Offering Percentage"}, {fields: "Allowed Locations", tables: "Courses", columns: "Allowed to Offer"} ]
or you can have a POJO represent that information
object
class UpdateMappingList {
List<UpdateDataMapping> information;
...
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论