简体   繁体   中英

Unable to parse JSON object to Java Object in Springs Controller

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.

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



}

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