JSON解析错误:无法反序列化ArrayList的实例

huangapple go评论93阅读模式
英文:

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(&quot;/dataMapping/update/table&quot;)

public ResponseEntity&lt;Object&gt; updateDataMappingTable(@RequestBody List&lt;UpdateDataMapping&gt; information) {

	try {
		String update = this.dataMappingService.update(information);
		return new ResponseEntity&lt;Object&gt;(update, HttpStatus.OK);
	} catch (Exception e) {
		Response response = new Response();
		response.setError(e.getMessage());
		return new ResponseEntity&lt;Object&gt;(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&lt;com.kastech.model.UpdateDataMapping&gt; out of START_OBJECT token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.ArrayList&lt;com.kastech.model.UpdateDataMapping&gt; 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: &quot;Periods Offered&quot;, tables: &quot;Courses&quot;, columns: &quot;Academic Level*&quot;}, {fields: &quot;Default Grading Basis*&quot;, tables: &quot;Courses&quot;, columns: &quot;Default Offering Percentage&quot;}, {fields: &quot;Allowed Locations&quot;, tables: &quot;Courses&quot;, columns: &quot;Allowed to Offer&quot;} ]

or you can have a POJO represent that information object

class UpdateMappingList {
    List&lt;UpdateDataMapping&gt; information;
    ...
}

huangapple
  • 本文由 发表于 2020年8月21日 02:35:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/63511273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定