作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在后期服务中,我使用以下方法来解析和更新数据库:
ObjectMapper mapper = new ObjectMapper();
JsonNode node = mapper.readTree(<String>);
UserLogin userLogin = mapper.convertValue(node.get("UserLogin"), UserLogin.class);
UserSecurityDetails userSecurityDetails = mapper.convertValue(node.get("UserSecurityDetails"), UserSecurity
现在,在获取服务中,我想通过从数据库检索并添加到 JSON 来发送相同的数据。谁能建议最好的方法是什么?
要形成的示例 JSON:
{
"UserLogin":
{
"user_login_id": "10011",
"user_password": "password"
},
"UserSecurityDetails":
{
"user_sequence_id": "1",
"seq_question_id": "1",
"seq_answer": "Test Answer"
}
}
最佳答案
创建一个具有 UserLogin
和 UserSecurityDetails
的包装 POJO。 Jackson 会自动反序列化
到您的对象。
It will be good practice to expect required Object instead of creating objects from String.
你的包装类将是这样的
public class SecurityDetailsWrapper {
private UserLogin;
private UserSecurityDetails;
// costructor
// getters and setters
}
在您的 Controller
方法中,您可以期待 SecurityDetailsWrapper
。
喜欢
public void someFunction(@RequestBody SecurityDetailsWrapper wrapper) {
// business logic
}
jackson 将负责反序列化
。
关于java - 如何将对象添加到 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29869356/
我是一名优秀的程序员,十分优秀!