Cannot deserialize instance of `java.lang.String` out of START_ARRAY token

发布时间 2023-06-02 08:17:45作者: zno2

 

反序列化时,字段接受非数组,但是传入的是数组

    @PostMapping(path = "/aa", produces = { "application/json;charset=UTF-8" }, consumes= {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object post(@RequestBody Foo foo) {
        return "{}";
    }
    
    public class Foo {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

 

序列使用的是 com.fasterxml.jackson.databind.ObjectMapper

 

测试:

curl -H "Content-Type:application/json;utf-8" -X POST -d "{\"name\":[\"1\",\"2\"]}" http://localhost:8888/aa

结果复现:

JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token; 
nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token
at [Source: (PushbackInputStream); line: 1, column: 9] (through reference chain: cn.zno.Foo[\"name\"])