Announcement Announcement Module
Collapse
No announcement yet.
Not able to receive jsonData in spring controller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Not able to receive jsonData in spring controller

    Hi Guys,
    I am using spring 3.0.5.
    I have simple json array and i want to receieve it in spring controller as list of state in this example

    Json data is as below
    Code:
    [{"stateCode":"45","stateDesc":"Tamilnadu ","stateCodeNumber":"0","stdCode":"7890","isEdited":"true"},{"stateCode":"45","stateDesc":"Tamilnadu ","stateCodeNumber":"0","stdCode":"7890","isEdited":"true"},{"stateCode":"45","stateDesc":"Tamilnadu ","stateCodeNumber":"0","stdCode":"7890","isEdited":"true"}]
    pojo
    Code:
    public class State {
    
    	// A member
    	private String stateDesc;
    	// A member
    	private String stateCode;
    	private Integer stdCode;
    	private Integer stateCodeNumber;
    
    	// refernced member variable
    
    	// getter and setter for refrenced entity
    
    	public Integer getStdCode() {
    		return stdCode;
    	}
    
    	public void setStdCode(Integer stdCode) {
    		this.stdCode = stdCode;
    	}
    
    	public Integer getStateCodeNumber() {
    		return stateCodeNumber;
    	}
    
    	public void setStateCodeNumber(Integer stateCodeNumber) {
    		this.stateCodeNumber = stateCodeNumber;
    	}
    
     
    
    	public State() {
    	 
    
    	}
    
    	// for referanced entity in constructor
    	public State(String stateDesc, String stateCode) {
    		this.stateDesc = stateDesc;
    		this.stateCode = stateCode;
    		// refernced member
    	}
    
    	// Generate the getters and setters for the fields
    	public void setStateDesc(String stateDesc) {
    		this.stateDesc = stateDesc;
    	}
    
    	public String getStateDesc() {
    		return this.stateDesc;
    	}
    
    	public void setStateCode(String stateCode) {
    		this.stateCode = stateCode;
    	}
    
    	public String getStateCode() {
    		return this.stateCode;
    	}
    
     
    }
    you will find some fields which are present in json are not found in pojo its because i dont require those fields they are simply redundant fields that i get from json.

    Also my jsp dosent as ModelAttribute i.e i am not using fpring form tags it is designed as simple html tags

    How can i get list of State in my controller ?

  • #2
    Hi,
    Got it solved:
    few things to take care if you are beginner
    1. json data should be of the form
    Code:
    {stateList:
    [{"stateCode":"45","stateDesc":"Tamilnadu ","stateCodeNumber":"0","stdCode":"7890","isEdited":"true"},{"stateCode":"45","stateDesc":"Tamilnadu ","stateCodeNumber":"0","stdCode":"7890","isEdited":"true"},{"stateCode":"45","stateDesc":"Tamilnadu ","stateCodeNumber":"0","stdCode":"7890","isEdited":"true"}]
    }
    2. Your object should contain ssetter and getter for the list. i.y spring controller should have a single object in as argument while that object should contain list of the objects that it needs to have
    In my case it is State { public setStateList public .. getStateList }

    3. Dont use json parameters whose properties are not present in the object it give an error if you want to use use annotation ignor-property // please check the annotation

    Thanks and Regards
    Patil Abhijeet

    Comment

    Working...
    X