Vimax
V2EX  ›  Java

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

  •  
  •   Vimax · Jul 12, 2020 · 5248 views
    This topic created in 2309 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
    xDaShen
    Jul 12, 2020❤️1
    1
    List<Integer>
    magiclz233
    magiclz233
    Jul 12, 2020❤️1
    2
    List<String>或者 List<Integer>, 里面传的是你的要删除的 id 集合, 如果是[{},{}]这种形式,List<Bean>
    itechify
    itechify
    Jul 12, 2020 via Android
    3
    印象 @RequestBody int[] ids 可以好像,List<Intege> ids 外面需要包一层?
    具体忘记了(#-.-)
    zhuawadao
    zhuawadao
    Jul 12, 2020❤️1
    4
    @JsonObject 试试可以吗
    paragon
    paragon
    Jul 12, 2020❤️1
    5
    RequestEntity<List<Long>> ids;
    ids.getBody();
    ilumer
    ilumer
    Jul 12, 2020❤️1
    6
    这是 spring mvc,在 body 里面直接接收数组
    {
    private List<Long> ids;
    }
    https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-requestbody
    EminemW
    EminemW
    Jul 12, 2020
    7
    你应该用 json 格式。。
    {
    "id":[]
    }
    cnzjl
    cnzjl
    Jul 12, 2020❤️1
    8
    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   ·   Privacy   ·   Solana   ·   1905 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 44ms · UTC 09:05 · PVG 17:05 · LAX 01:05 · JFK 04:05
    ♥ Do have faith in what you're doing.