0

I am wrestling with this JSON to Java Object conversion. Here is my client side JavaScript code where it sends the JSON to Ajax call.

var college = {
        collegeName : $("#collegeNameID").val(),
        estYear : $("#estYear").val(),
        universityOrBoardName : $("#university").val(),
        website : $("#webSite").val()
    };
    var address = { 
        city    :  $("#cityID").val(),
        district:  $("districtID").val()
    };
    ajaxResult = $.ajax({
        url : urls,
        async : false,
        cache : false,
        type : "POST",
        dataType : "json",
        contentType : "application/json",
        mimeType : "application/json",
        data : JSON.stringify({ College : college, Address: address}),
        mimeType : "application/json",
        success : function(result) {
        return result;
    }

Here is my Pojo class which are mapped to the respective JSON.

Address.java

private String district;
private String city;
    (there are other member variables for which I will not be getting the input from the Client side. I will be setting those in server side)

College.java

private String collegeName;
private String universityOrBoardName;
private String website;
  (there are other member variables for which I will not be getting the input from the Client side. I will be setting those in server side)

In my Controller code, the string JSON value coming from the Client code is

{"College":{"collegeName":"sfd","universityOrBoardName":"sdd","website":"fdd"}}

My Controller code

CollegeController.java

@RequestMapping(value="/submitCol", method = RequestMethod.POST)
    public  String mycollege(@RequestBody String str,HttpServletRequest req)
    {
             Gson gson=new Gson();
    Type type = new TypeToken<College>(){}.getType();
    College cols=gson.fromJson(str, type);
    System.out.println("College Name "+cols.getCollegeName());//HERE ITS PRINTING NULL

}

So my question is..I am sending two JSONs namely Address, College from Client side and in my controller I am trying to convert them into their respective Java Value Objects( Java Beans). But I am not able to do it. Please help.

2
  • 1
    The client should not be sending the outer json object, ie. the one containing College. It should only be sending the JSON object named College. Commented May 17, 2014 at 5:12
  • {"collegeName": "sfd", "universityOrBoardName":"sdd","website":"fdd"} is what the controller expects. Commented May 17, 2014 at 5:15

1 Answer 1

1

Create Class to hold both POJO's say it be RequestCommand:

 class RequestCommand{

      private College college;

      private Address address;

      // follows getter and setter


  }

Your Address and College POJO will automatically be binded inside RequestCommand by Spring Framework to controller method Params like below:

@RequestMapping(value="/submitCol", method = RequestMethod.POST)
public  String mycollege(@ModelAttribute RequestCommand command)
{
      // use both of them here

       command.getCollege();



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

2 Comments

Why do you think so and where would Address come from?
Can @ModelAttribute deserialize json?

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.