简体   繁体   中英

How to iterate over the arrayList of objects in the java?

I have used angular to send the array of objects to Api for post operation which is going with Arrays of 3 Objects:

在此处输入图片说明 I wanted this in java so i initialized class in java as:

SLDto.java

 public class SLDto {

        private LetterDto letterDto;
        private List<DocumentDto> documentDto;
        private List<SelectionCustomOfficeDto> selectionCustomOfficeDto;



        public SLDto() {

        }
//i omitted getters and setters here
    }

LetterDto.java

public class LetterDto {

  private int clkletter;
 private String inOut;
 private String inOutNo;
private String inOutDate;
private String letterIssuedSubBy;
private String letterFile;
private String representativeName;
private int assessmentNo;
private int selectionNo;


public LetterDto() {

}

DocumentDto.java

public class DocumentDto {

    private int docId;
    private String docName;
    private boolean checked;

    public DocumentDto() {

    }
}

SelectionCustomOfficeDto.java

public class SelectionCustomOfficeDto {

 private int id;
 private String fromDate;
 private String  toDate;
 private int consignmentNo;
 private int selectionId;
 private int customOfficeId;
 private String custOfficeName;
 private String selectionName;
}

i need to map the Client side Objects to Api so i used the method :

 @PostMapping(value = "/letter/create")
        public String postAllOne(@RequestBody SLDto sldto ) {

            //i tried 2ways to see the json data or trace it and assign into 
         respective objects but i am not getting.I tried
        1st method
       System.out.println(sldto.getLetterDto()); //Not working 

       2nd method 
         for(LetterDto letterDto:sldto.getLetterDto()) {
                //it is not allowing me
            }

            return  "success";
        } 

it is not allowing me to map as:

在此处输入图片说明

How can i separate the 3json data into their respective Object?

[{"inOutNo":"2018-11-12","inOutDate":"2","inOut":"AnnexOne","letterFile":null,"representativeName":null,"assessmentNo":0,"letterIssuedSubBy":null,"selectionNo":8},[{"docId":1,"docName":"proforma invoice","checked":true},{"docId":2,"docName":"Packing list","checked":true}],[{"customOfficeId":"1","fromDate":"2018-11-12","toDate":"2018-11-20","consignmentNo":2,"selectionId":8,"selectionName":"PCS","custOfficeName":"Bhairawa Bhansar"}]] 

The error seen is

I am getting error like

"DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of com.ashwin.springsecurityangular.dto.SLDto out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of com.ashwin.springsecurityangular.dto.SLDto out of START_ARRAY token"

You should send like json like below:

 {
    "letterDto" : {"clkletter": "as" }, // your other properties,
    "documentDto": [{"docId" : 1},{"docId" : 2}]// your other properties inside json object
    "selectionCustomOfficeDto": [{"id": 12},{"id": 121}]// your other properties inside json object

 }

The below is example for your reference, The outer class has List<Inner2> , List<Inner3> and Inner1

The Json would looks like

{
    "inner1": {"f1": "v1","f2": "v2"},
    "inner2s": [{"f3": "v3","f4": "v4"},{"f3": "v5","f4": "v6"}],

    "inner3s": [{"f5": "v7","f6": "v8"},{"f5": "v9","f6": "v10"}]
}

PoJo classes

class Outer{
        Inner1 inner1;
        List<Inner2> inner2s;
        List<Inner3> inner3s;

        public Inner1 getInner1() {
            return inner1;
        }
        public void setInner1(Inner1 inner1) {
            this.inner1 = inner1;
        }
        public List<Inner2> getInner2s() {
            return inner2s;
        }
        public void setInner2s(List<Inner2> inner2s) {
            this.inner2s = inner2s;
        }
        public List<Inner3> getInner3s() {
            return inner3s;
        }
        public void setInner3s(List<Inner3> inner3s) {
            this.inner3s = inner3s;
        }

    }

     class Inner1{

        String f1;
        String f2;
        public String getF1() {
            return f1;
        }
        public void setF1(String f1) {
            this.f1 = f1;
        }
        public String getF2() {
            return f2;
        }
        public void setF2(String f2) {
            this.f2 = f2;
        }



    }
     class Inner2{
        String f3;
        String f4;
        public String getF3() {
            return f3;
        }
        public void setF3(String f3) {
            this.f3 = f3;
        }
        public String getF4() {
            return f4;
        }
        public void setF4(String f4) {
            this.f4 = f4;
        }


    }

     class Inner3{
        String f5;
        String f6;
        public String getF5() {
            return f5;
        }
        public void setF5(String f5) {
            this.f5 = f5;
        }
        public String getF6() {
            return f6;
        }
        public void setF6(String f6) {
            this.f6 = f6;
        }


    }

The request mapping

@RequestMapping(value="/test",produces=MediaType.APPLICATION_JSON_VALUE,method= {RequestMethod.POST})
    public String post(@RequestBody Outer outer) {
        LOGGER.debug("Getting the logged in cutomer details" +outer);
        Customer customer1 = new Customer("1", "customer1", "Sample@cust1.com");
        LOGGER.info("The customer details are " + customer1);
        return "done!!";
    }

Your current POST method handler expects a JSON object of the following format:

{
    "letterDto" : {
        "clkletter" : 1, 
        "inOut" : "Someting",
        ...
    },
    "documentDto" : [
        {
            "docId" : 1,
            "docName" : "Name",
            "checked" : true
        } ,
        {
            "docId" : 2,
            "docName" : "Name 2",
            "checked" : false
        }
    ],
    "selectionCustomOfficeDto" : [
        {
            "id" : 1,
            "fromDate" : "someDate,
            ...
        },
        {
            "id" : 2,
            "fromDate" : "someDate2,
            ...
        }
    ]

}

But currently, you are sending a JSON array:

[
    "letterDto" : {<letter properties>},
    [
        {
            <document properties>
        },
        {
            <document properties>
        }
    ],
    [
        {
            <selection custom office properties>
        }
    ]
]

After fixing this, you will be able to iterate over slDto.getDocumentDto and slDto.getSelectionCustomOfficeDto as those are the only collections contained in the slDto object, so:

slDto.getSelectionCustomOfficeDto.forEach(s -> doSomething(s));

slDto.getDocumentDto.forEach(d -> doSomething(d));

And you can call the Letter 's methods like this:

slDto.getLetterDto.getId();

Hi please make sure your JSON request looks like this :

{
    "letterDto" : {
        "clkletter" : 1,
        "inOut" : "some-string-value",
        "inOutNo" : "some-string-value",
        "inOutDate" : "some-string-value",
        "letterIssuedSubBy" : "some-string-value",
        "letterFile" : "some-string-value",
        "representativeName" : "some-string-value",
        "assessmentNo" : 1,
        "selectionNo" : 1
    },
    "documentDto" : [
        {
            "docId" : 1,
            "docName" : "some-doc-name",
            "checked" : true
        },
        {
            "docId" : 2,
            "docName" : "some-doc-name",
            "checked" : true
        },
        {
            "docId" : 3,
            "docName" : "some-doc-name",
            "checked" : true
        }
    ],
    "selectionCustomOfficeDto" : [
        {
            "id" : 1,
            "fromDate" : "some-date",
            "toDate" : "some-date",
            "consignmentNo" : 1,
            "selectionId" : 1,
            "customOfficeId" : 1,
            "custOfficeName" : "some-office-name",
            "selectionName" : "some-selection-name"
        },
        {
            "id" : 2,
            "fromDate" : "some-date",
            "toDate" : "some-date",
            "consignmentNo" : 1,
            "selectionId" : 1,
            "customOfficeId" : 1,
            "custOfficeName" : "some-office-name",
            "selectionName" : "some-selection-name"
        },
        {
            "id" : 3,
            "fromDate" : "some-date",
            "toDate" : "some-date",
            "consignmentNo" : 1,
            "selectionId" : 1,
            "customOfficeId" : 1,
            "custOfficeName" : "some-office-name",
            "selectionName" : "some-selection-name"
        }
    ]
}

Also, in the code block,

for(LetterDto letterDto:sldto.getLetterDto()) {
//it is not allowing me
}

Here sldto.getLetterDto() return a single LetterDto Object instead of a List , so iteration is not possible.

Hope it helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM