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
}
7
  • 1
    What is the exception? Commented Apr 27, 2016 at 8:32
  • why don't create bean for json object? Commented Apr 27, 2016 at 8:33
  • @asg Sorry for that. I have updated the question. Commented Apr 27, 2016 at 8:36
  • 1
    Create a POJO that matches the given json, i.e. with 2 lists of objects (preferable one that represents connect and node) and a String Commented Apr 27, 2016 at 8:38
  • @dambros can you give me an idea of how will i process json data with that pojo? Commented Apr 27, 2016 at 8:48

1 Answer 1

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.

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

9 Comments

let me try this one.
This should get the got done. Just receive this POJO as your controller parameter.
It's giving : no single-String constructor/factory method exception
Please add the complete root cause.
|

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.