dec*_*rog 6 java arrays json spring-boot
我在spring boot中有一个端点,它使用这个JSON作为例子:
{
"userId": 3,
"postBody": "This is the body of a post",
"postTitle": "This is the title of a post",
"created": null,
"tagList": ["tag1", "tag2", "tag3"]
}
Run Code Online (Sandbox Code Playgroud)
终点:
@RequestMapping(value="/newPost", method = RequestMethod.POST, produces="application/json", consumes = "application/json")
@ResponseBody
public ResponseEntity newPost(@RequestBody Map<String, Object> body) throws Exception {
Run Code Online (Sandbox Code Playgroud)
我知道这里的问题是Request body被保存为对象Map,除了tagList之外的所有其他属性都可以.如何让tagList成为Java中的字符串数组?
谢谢.
Ankur和Jose的答案混合在一起解决了这个问题,感谢快速响应的人!
您可能应该创建一个代表输入JSON的Java类,并在方法中使用它newPost(.....)。例如:-
public class UserPostInfo {
private int userId;
private String postBody;
private String postTitle;
private Date created;
private List<String> tagList;
}
Run Code Online (Sandbox Code Playgroud)
另外,在此类中包括getter / setter方法。如果要修改JSON解析的行为,则可以使用注释来更改字段名称,仅包括非null值,以及类似的内容。
| 归档时间: |
|
| 查看次数: |
7210 次 |
| 最近记录: |