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










0















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



enter image description here
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:



enter image description here



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"











share|improve this question



















  • 2





    as per your class defination letterDto is not a collection nor an array. is it a typo?

    – secret super star
    Nov 16 '18 at 9:47












  • 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"

    – ashwin karki
    Nov 16 '18 at 9:50











  • Ok - there seem to compile time error in your post and hence the question was.

    – secret super star
    Nov 16 '18 at 9:52











  • can you post input json

    – secret super star
    Nov 16 '18 at 9:52











  • i have posted a image at beginning of page please see it

    – ashwin karki
    Nov 16 '18 at 9:53















0















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



enter image description here
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:



enter image description here



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"











share|improve this question



















  • 2





    as per your class defination letterDto is not a collection nor an array. is it a typo?

    – secret super star
    Nov 16 '18 at 9:47












  • 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"

    – ashwin karki
    Nov 16 '18 at 9:50











  • Ok - there seem to compile time error in your post and hence the question was.

    – secret super star
    Nov 16 '18 at 9:52











  • can you post input json

    – secret super star
    Nov 16 '18 at 9:52











  • i have posted a image at beginning of page please see it

    – ashwin karki
    Nov 16 '18 at 9:53













0












0








0








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



enter image description here
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:



enter image description here



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"











share|improve this question
















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



enter image description here
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:



enter image description here



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"








java arrays json spring spring-boot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 12:24









secret super star

1,026316




1,026316










asked Nov 16 '18 at 9:42









ashwin karkiashwin karki

23212




23212







  • 2





    as per your class defination letterDto is not a collection nor an array. is it a typo?

    – secret super star
    Nov 16 '18 at 9:47












  • 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"

    – ashwin karki
    Nov 16 '18 at 9:50











  • Ok - there seem to compile time error in your post and hence the question was.

    – secret super star
    Nov 16 '18 at 9:52











  • can you post input json

    – secret super star
    Nov 16 '18 at 9:52











  • i have posted a image at beginning of page please see it

    – ashwin karki
    Nov 16 '18 at 9:53












  • 2





    as per your class defination letterDto is not a collection nor an array. is it a typo?

    – secret super star
    Nov 16 '18 at 9:47












  • 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"

    – ashwin karki
    Nov 16 '18 at 9:50











  • Ok - there seem to compile time error in your post and hence the question was.

    – secret super star
    Nov 16 '18 at 9:52











  • can you post input json

    – secret super star
    Nov 16 '18 at 9:52











  • i have posted a image at beginning of page please see it

    – ashwin karki
    Nov 16 '18 at 9:53







2




2





as per your class defination letterDto is not a collection nor an array. is it a typo?

– secret super star
Nov 16 '18 at 9:47






as per your class defination letterDto is not a collection nor an array. is it a typo?

– secret super star
Nov 16 '18 at 9:47














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"

– ashwin karki
Nov 16 '18 at 9:50





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"

– ashwin karki
Nov 16 '18 at 9:50













Ok - there seem to compile time error in your post and hence the question was.

– secret super star
Nov 16 '18 at 9:52





Ok - there seem to compile time error in your post and hence the question was.

– secret super star
Nov 16 '18 at 9:52













can you post input json

– secret super star
Nov 16 '18 at 9:52





can you post input json

– secret super star
Nov 16 '18 at 9:52













i have posted a image at beginning of page please see it

– ashwin karki
Nov 16 '18 at 9:53





i have posted a image at beginning of page please see it

– ashwin karki
Nov 16 '18 at 9:53












3 Answers
3






active

oldest

votes


















1














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!!";






share|improve this answer

























  • Please reason for downvote, so, if something wrong, that can be learnt. Thanks

    – secret super star
    Nov 16 '18 at 10:54











  • I made my array to looki like this :

    – ashwin karki
    Nov 16 '18 at 11:15











  • please see the edit post if it is right say me?

    – ashwin karki
    Nov 16 '18 at 11:16











  • sorry - I could not follow you

    – secret super star
    Nov 16 '18 at 11:19











  • thnk u it fianlly worked :)

    – ashwin karki
    Nov 16 '18 at 11:22


















1














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!






share|improve this answer






























    1














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





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335139%2fhow-to-iterate-over-the-arraylist-of-objects-in-the-java%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      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!!";






      share|improve this answer

























      • Please reason for downvote, so, if something wrong, that can be learnt. Thanks

        – secret super star
        Nov 16 '18 at 10:54











      • I made my array to looki like this :

        – ashwin karki
        Nov 16 '18 at 11:15











      • please see the edit post if it is right say me?

        – ashwin karki
        Nov 16 '18 at 11:16











      • sorry - I could not follow you

        – secret super star
        Nov 16 '18 at 11:19











      • thnk u it fianlly worked :)

        – ashwin karki
        Nov 16 '18 at 11:22















      1














      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!!";






      share|improve this answer

























      • Please reason for downvote, so, if something wrong, that can be learnt. Thanks

        – secret super star
        Nov 16 '18 at 10:54











      • I made my array to looki like this :

        – ashwin karki
        Nov 16 '18 at 11:15











      • please see the edit post if it is right say me?

        – ashwin karki
        Nov 16 '18 at 11:16











      • sorry - I could not follow you

        – secret super star
        Nov 16 '18 at 11:19











      • thnk u it fianlly worked :)

        – ashwin karki
        Nov 16 '18 at 11:22













      1












      1








      1







      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!!";






      share|improve this answer















      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!!";







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Nov 16 '18 at 10:50

























      answered Nov 16 '18 at 10:39









      secret super starsecret super star

      1,026316




      1,026316












      • Please reason for downvote, so, if something wrong, that can be learnt. Thanks

        – secret super star
        Nov 16 '18 at 10:54











      • I made my array to looki like this :

        – ashwin karki
        Nov 16 '18 at 11:15











      • please see the edit post if it is right say me?

        – ashwin karki
        Nov 16 '18 at 11:16











      • sorry - I could not follow you

        – secret super star
        Nov 16 '18 at 11:19











      • thnk u it fianlly worked :)

        – ashwin karki
        Nov 16 '18 at 11:22

















      • Please reason for downvote, so, if something wrong, that can be learnt. Thanks

        – secret super star
        Nov 16 '18 at 10:54











      • I made my array to looki like this :

        – ashwin karki
        Nov 16 '18 at 11:15











      • please see the edit post if it is right say me?

        – ashwin karki
        Nov 16 '18 at 11:16











      • sorry - I could not follow you

        – secret super star
        Nov 16 '18 at 11:19











      • thnk u it fianlly worked :)

        – ashwin karki
        Nov 16 '18 at 11:22
















      Please reason for downvote, so, if something wrong, that can be learnt. Thanks

      – secret super star
      Nov 16 '18 at 10:54





      Please reason for downvote, so, if something wrong, that can be learnt. Thanks

      – secret super star
      Nov 16 '18 at 10:54













      I made my array to looki like this :

      – ashwin karki
      Nov 16 '18 at 11:15





      I made my array to looki like this :

      – ashwin karki
      Nov 16 '18 at 11:15













      please see the edit post if it is right say me?

      – ashwin karki
      Nov 16 '18 at 11:16





      please see the edit post if it is right say me?

      – ashwin karki
      Nov 16 '18 at 11:16













      sorry - I could not follow you

      – secret super star
      Nov 16 '18 at 11:19





      sorry - I could not follow you

      – secret super star
      Nov 16 '18 at 11:19













      thnk u it fianlly worked :)

      – ashwin karki
      Nov 16 '18 at 11:22





      thnk u it fianlly worked :)

      – ashwin karki
      Nov 16 '18 at 11:22













      1














      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!






      share|improve this answer



























        1














        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!






        share|improve this answer

























          1












          1








          1







          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!






          share|improve this answer













          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!







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 10:50









          Abhishek RoyAbhishek Roy

          3529




          3529





















              1














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





              share|improve this answer





























                1














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





                share|improve this answer



























                  1












                  1








                  1







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





                  share|improve this answer















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






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 16 '18 at 10:51

























                  answered Nov 16 '18 at 10:44









                  BradBrad

                  1969




                  1969



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53335139%2fhow-to-iterate-over-the-arraylist-of-objects-in-the-java%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      政党

                      天津地下鉄3号線