Search code examples
javaspringspring-bootdto

Spring boot variable/nested request body


I am working on a Spring Boot application that allows users to upload logic expression to the server. A web app allows them to visually create the expression. The result of the web app looks something like this

{
  "id": "someId",
  "fullexpression": {
    "left": "a",
    "operator": "AND",
    "right": {
        "left": {
            "left": "x",
            "operator": "AND",
            "right": "y",
        }
        "operator": "OR",
        "right": "z"
    }
  }
}

The above example describes the expression a AND ((x AND y) OR z).

I've found a Baeldung article that says:

the type we annotate with the @RequestBody annotation must correspond to the JSON sent from our client-side controller

If I understood the article correctly, it's not possible to do this directly. What is the best way to build a Spring Boot rest controller that allows a RequestBody to be nested like this? Of course I could always turn the JSON into a string on the client's side, and parse it in the rest controller but that doesn't really seem elegant.


Solution

  • Define the RequestBody parameter as a JsonNode:

    public <something> myService(@RequestBody JsonNode jsonNode) {}