我的弹簧控制器中有多个单独的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成功函数中获得这些个体arraylist
答案 0 :(得分:1)
您只需返回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);
}
}
结果:[[&#34; l11&#34;,&#34; l12&#34;,&#34; l13&#34;],[&#34; l21&#34;,&#34; l22& #34;&#34; L23&#34;],[&#34; L31&#34;&#34; L32&#34;&#34; L33&#34;]]
或者创建一个封装这些的DTO并直接返回它以获取包含param名称的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;
}
}
}
结果: {&#34; LI&#34;:[&#34; L11&#34;&#34; L12&#34;&#34; L13&#34],&#34; L2&#34;:[& #34; L21&#34;&#34; L22&#34;&#34; L23&#34],&#34; L3&#34;:[&#34; L31&#34;&#34; L32&#34;&#34; L33&#34;]}
答案 1 :(得分:0)
您可以创建一个类并将ArrayLists放在其中,而不是在控制器中返回此类。
public class ClassName {
List<String> l1 = new ArrayList<>();
List<String> l2 = new ArrayList<>();
List<String> l3 = new ArrayList<>();
// Getters and Setters
}
public @ResponseBody ClassName deleteFileFromS3(){
ClassName lists = new ClassName();
// set lists
return lists;
}