1

I am sending json from ajax to controller that contain multiple array.

Json Data:

{
    "node": [
        {
            "id": "dev_1",
            "blockId": "11"
        },
        {
            "id": "dev_2",
            "blockId": "15"
        }
    ],
    "connect": [
        {
            "id": "con_5",
            "typeId": "2"
        }
    ],
    "name": "test"
}

Controller:

@RequestMapping(value = "/saveBoard", method = RequestMethod.POST)
    public JsonResponse saveBoard(@RequestBody String jsonData) throws IOException {
        JsonResponse response = new JsonResponse();
        ObjectMapper mapper = new ObjectMapper();
        final JsonNode jsonNode = mapper.readTree(jsonData).get("node");

        if(jsonNode.isArray()) {
            for (final JsonNode nodes : jsonNode) {
                System.out.println("jsonNode : "+ nodes);
            }
        }
        return response;
    }

I have tried with object mapper but not succeed. Here i want to read different array for different classes like node for node class with some specified fields, connect for connect class and string for another use.

UPDATE Contorller:

@RequestMapping(value = "/saveBoard", method = RequestMethod.POST)
    public JsonResponse saveMissionBoard(@RequestBody MissionJsonPojo chartJson) {
        boolean status = false;
        String messsage = "";
        JsonResponse response = new JsonResponse();
        System.out.println("data : " + flowChartJson.getNodes());        
        return response;
    }

Ajax:

 $.ajax({
          url: '<c:url value="/board/saveBoard"/>',
          type: 'POST',
          data: JSON.stringify(chartJson),
          dataType: "json",
          contentType: "application/json",
          success: function(response) {
            console.log("In success");
          },
          error: function (a, b, c) {  }
        });

JSON:

{
    "nodes": [
        {
            "missionDeviceId": "device_1",
            "device": "1",
            "deviceXCoordinate": 79,
            "deviceYCoordinate": 73,
            "blockId": "1##1"
        },
        {
            "missionDeviceId": "device_2",
            "device": "3",
            "deviceXCoordinate": 340,
            "deviceYCoordinate": 146,
            "blockId": "2##5"
        }
    ],
    "connections": [
        {
            "missionConnectionId": "con_5",
            "sourceId": "device_1",
            "targetId": "device_2",
            "device": "2"
        }
    ],
    "name": "test"
}

Node Pojo:

public class Nodes{
    private String missionDeviceId;
    private Integer device;
    private String deviceXCoordinate;
    private String deviceYCoordinate;
    private String blockId; //getters setters
}

Connection Pojo:

public class Connections{    
    private String missionConnectionId;
    private String sourceId;
    private String targetId;
    private Integer device; //getters and setters
}

MissionJsonPojo:

public class MissionJsonPojo{
    private List<Nodes> nodes;
    private List<Connections> connections;
    private String name; //getters and setters
}
Jennifer
  • 351
  • 6
  • 18

1 Answers1

2

As suggested by @dambros, create a POJO structure like this:

public class Node {
    private String id;
    private String blockId;
    //setter-getters.
}

public class Connect {
    private String id;
    private String typeId;
    //setter-getters.
}

import java.util.List;

public class Payload {
    private List<Node> nodes;
    private List<Connect> connects;
    private String name;
    //setter-getters
}

And change your method signature to:

public JsonResponse saveBoard(@RequestBody Payload payload) throws IOException {

}

This should solve your problem.

asg
  • 2,248
  • 3
  • 18
  • 26
  • let me try this one. – Jennifer Apr 27 '16 at 08:56
  • 2
    This should get the got done. Just receive this POJO as your controller parameter. – dambros Apr 27 '16 at 09:00
  • It's giving : no single-String constructor/factory method exception – Jennifer Apr 28 '16 at 05:00
  • Please add the complete root cause. – asg Apr 28 '16 at 05:05
  • Take a look at - http://stackoverflow.com/questions/8369260/jackson-throws-jsonmappingexception-on-deserialize-demands-single-string-constr – asg Apr 28 '16 at 05:06
  • When request come to controller it gives the following exception: nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type – Jennifer Apr 28 '16 at 05:19
  • @asg I have updated my question with controller and ajax request, please have a look – Jennifer Apr 28 '16 at 05:23
  • @Jennifer It might be the problem with the JSON that you are sending to controller. Could you please paste - chartJson? – asg Apr 28 '16 at 05:29
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110463/discussion-between-jennifer-and-asg). – Jennifer Apr 28 '16 at 06:29