1

I have multiple individual arrayList's in my spring controller.

@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>();   
}

how do i get those individual arraylist's in ajax success function

2 Answers 2

1

You can simply return a List<List<String>> to get an array of arrays in json but you won't have the names and you'll have to be based on the order:

@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"]]

or alternatively create a DTO encapsulating these and return it directly to get a json object including the param names:

@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"]}

Sign up to request clarification or add additional context in comments.

1 Comment

How can i retrieve in ajax success function
0

You can create a class and put the ArrayLists inside it and than you return this class in the controller.

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.