Map JSON array of objects to @RequestBody List using jackson

前端 未结 3 1353
慢半拍i
慢半拍i 2020-12-13 14:36

I\'m having issues using Jackson to map a Javascript posted JSON array of hashes (Tag).


Here is the data received by the controller @RequestBody (It is send wit

相关标签:
3条回答
  • 2020-12-13 15:19

    Another way to do this is to rather obtain an array than a List, as follows:

    @RequestBody Tag[] entities
    
    0 讨论(0)
  • 2020-12-13 15:26

    Jackson requires a default constructor with no parameters on custom Objects, so you'll need to simply add a default constructor to your Tag class.

    In your case simply add to your Tag class:

    public Tag(){}
    
    0 讨论(0)
  • 2020-12-13 15:34

    It sounds like Spring is not passing full type information for some reason, but rather a type-erased version, as if declaration was something like List<?> tag. I don't know what can be done to fully resolve this (may need something from Spring integration team), but one work-around is to define your own type like:

    static class TagList extends ArrayList<Tag> { }
    

    and use that instead. This will retain generic parameterization through super-type declarations so that even if Spring only passes equivalent of TagList.class, Jackson can figure out the Tag parameter.

    0 讨论(0)
提交回复
热议问题