gpt4 book ai didi

java - 如何从 Controller 发送多个数组列表到ajax成功函数

转载 作者:行者123 更新时间:2023-11-30 06:14:14 24 4
gpt4 key购买 nike

我的 spring Controller 中有多个单独的 arrayList。

@RequestMapping(value = "/deleteFileFromS31")
public @ResponseBody List<String> deleteFileFromS3(){
ArrayList<String> l1=new ArrayList<String>();
ArrayList<String> l2=new ArrayList<String>();
ArrayList<String> l3=new ArrayList<String>();
}

我如何在ajax成功函数中获取这些单独的数组列表

最佳答案

您只需返回 List<List<String>>要获取 json 中的数组,但您没有名称,并且必须基于顺序:

@RestController
public class MyController {

@RequestMapping(path = "/hello")
public List<List<String>> path() {
List<String> l1 = Arrays.asList("l11","l12","l13");
List<String> l2 = Arrays.asList("l21","l22","l23");
List<String> l3 = Arrays.asList("l31","l32","l33");
return Arrays.asList(l1,l2,l3);
}
}

Result: [["l11","l12","l13"],["l21","l22","l23"],["l31","l32","l33"]]

或者创建一个封装这些的 DTO 并直接返回它以获取包含参数名称的 json 对象:

@RestController
public class MyController {

@RequestMapping(path = "/hello")
public DTO path() {
List<String> l1 = Arrays.asList("l11","l12","l13");
List<String> l2 = Arrays.asList("l21","l22","l23");
List<String> l3 = Arrays.asList("l31","l32","l33");
return new DTO(l1,l2,l3);
}

public static class DTO {
private final List<String> l1;
private final List<String> l2;
private final List<String> l3;

public DTO(List<String> l1, List<String> l2, List<String> l3) {
this.l1 = l1;
this.l2 = l2;
this.l3 = l3;
}

public List<String> getL1() {
return l1;
}

public List<String> getL2() {
return l2;
}

public List<String> getL3() {
return l3;
}
}
}

Result: {"l1":["l11","l12","l13"],"l2":["l21","l22","l23"],"l3":["l31","l32","l33"]}

关于java - 如何从 Controller 发送多个数组列表到ajax成功函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49587195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com