[英]Spring boot variable/nested request body
I am working on a Spring Boot application that allows users to upload logic expression to the server.我正在开发一个 Spring Boot 应用程序,它允许用户将逻辑表达式上传到服务器。 A web app allows them to visually create the expression.
网络应用程序允许他们直观地创建表达式。 The result of the web app looks something like this
Web 应用程序的结果如下所示
{
"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) .上面的例子描述了表达式a AND ((x AND y) OR z) 。
I've found a Baeldung article that says:我找到了一篇 Baeldung 文章,上面写着:
the type we annotate with the @RequestBody annotation must correspond to the JSON sent from our client-side controller
我们用@RequestBody 注解注解的类型必须对应于从我们的客户端控制器发送的 JSON
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?构建允许像这样嵌套
RequestBody
的 Spring Boot rest 控制器的最佳方法是什么? 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.当然,我总是可以在客户端将 JSON 转换为字符串,然后在 rest 控制器中解析它,但这看起来并不优雅。
将 RequestBody 参数定义为 JsonNode:
public <something> myService(@RequestBody JsonNode jsonNode) {}
You might use a class that looks like :您可能会使用如下所示的类:
public class Form {
private String id;
private Expression fullexpression;
// constructor, getters and setters
}
where the class Expression has the fields left, operator (String), and right.其中类 Expression 有字段 left、operator (String) 和 right。
Since the type of left and right fields can be String or Expression, I would suggest you to set their type to Map<String,Expression> or JsonNode.. but I didn't test yet.由于左右字段的类型可以是字符串或表达式,我建议您将它们的类型设置为 Map<String,Expression> 或 JsonNode.. 但我还没有测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.