V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
Vimax
V2EX  ›  Java

请问 springboot 如何接收 json 数组?比如根据 ID 批量删除?

  •  
  •   Vimax · Jul 12, 2020 · 4752 views
    This topic created in 2118 days ago, the information mentioned may be changed or developed.

    请问根据 ID 批量删除,前端传入的是 JSON 数组,比如

    [1,2,3]
    

    传统在 URL 上传参是可以直接通过数组接收,但是通过 JSON 传参,则需要使用 @RequstBody 接收,但是接收的数据类型设置为什么呢?

    前端在数组里面传对象,也可以接收

    [{"id:1},{"id":2}]
    

    但是后端如何接收

    [1,2,3]
    

    这样的 json 数据?

    8 replies    2020-07-12 21:55:18 +08:00
    xDaShen
        1
    xDaShen  
       Jul 12, 2020   ❤️ 1
    List<Integer>
    magiclz233
        2
    magiclz233  
       Jul 12, 2020   ❤️ 1
    List<String>或者 List<Integer>, 里面传的是你的要删除的 id 集合, 如果是[{},{}]这种形式,List<Bean>
    itechify
        3
    itechify  
    PRO
       Jul 12, 2020 via Android
    印象 @RequestBody int[] ids 可以好像,List<Intege> ids 外面需要包一层?
    具体忘记了(#-.-)
    zhuawadao
        4
    zhuawadao  
       Jul 12, 2020   ❤️ 1
    @JsonObject 试试可以吗
    paragon
        5
    paragon  
       Jul 12, 2020   ❤️ 1
    RequestEntity<List<Long>> ids;
    ids.getBody();
    ilumer
        6
    ilumer  
       Jul 12, 2020   ❤️ 1
    这是 spring mvc,在 body 里面直接接收数组
    {
    private List<Long> ids;
    }
    https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestbody
    EminemW
        7
    EminemW  
       Jul 12, 2020
    你应该用 json 格式。。
    {
    "id":[]
    }
    cnzjl
        8
    cnzjl  
       Jul 12, 2020   ❤️ 1
    get 请求前端传递字符串用,分割, post 请求前端直接传递数组如:[1,2,3,4] ,直接 @RequestBody 就好了
    ```java
    @RequestMapping(value = "array",method = RequestMethod.POST)
    @ResponseBody
    public String array(@RequestBody List<Integer> ids){
    return ids.toString();
    }
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1137 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 23:59 · PVG 07:59 · LAX 16:59 · JFK 19:59
    ♥ Do have faith in what you're doing.