![](/img/trans.png)
[英]How to convert a JSON with array elements to a Java Object on Spring Boot
[英]Spring Boot - JSON Object Array to Java Array
我在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"]
}
终点:
@RequestMapping(value="/newPost", method = RequestMethod.POST, produces="application/json", consumes = "application/json")
@ResponseBody
public ResponseEntity newPost(@RequestBody Map<String, Object> body) throws Exception {
我知道这里的问题是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;
}
此外,在此类中包含getter / setter方法。 如果要修改JSON解析的行为,可以使用Annotations更改字段名称,仅包含非空值以及类似的内容。
您可以为使用String[]
与List<String>
的请求创建自定义Java POJO。 在这里,我使用网站jsonschema2pojo为你做了 。
package com.stackoverflow.question;
import com.fasterxml.jackson.annotation.*;
import java.util.HashMap;
import java.util.Map;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"userId",
"postBody",
"postTitle",
"created",
"tagList"
})
public class MyRequest {
@JsonProperty("userId")
private int userId;
@JsonProperty("postBody")
private String postBody;
@JsonProperty("postTitle")
private String postTitle;
@JsonProperty("created")
private Object created;
@JsonProperty("tagList")
private String[] tagList = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("userId")
public int getUserId() {
return userId;
}
@JsonProperty("userId")
public void setUserId(int userId) {
this.userId = userId;
}
@JsonProperty("postBody")
public String getPostBody() {
return postBody;
}
@JsonProperty("postBody")
public void setPostBody(String postBody) {
this.postBody = postBody;
}
@JsonProperty("postTitle")
public String getPostTitle() {
return postTitle;
}
@JsonProperty("postTitle")
public void setPostTitle(String postTitle) {
this.postTitle = postTitle;
}
@JsonProperty("created")
public Object getCreated() {
return created;
}
@JsonProperty("created")
public void setCreated(Object created) {
this.created = created;
}
@JsonProperty("tagList")
public String[] getTagList() {
return tagList;
}
@JsonProperty("tagList")
public void setTagList(String[] tagList) {
this.tagList = tagList;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
如果您不想使用自定义POJO,您也可以自己将反序列化处理为Map。 让你的控制器接受一个String
,然后使用Jackson的ObjectMapper
和TypeReference
来获取一张地图。
@RequestMapping(value="/newPost", method = RequestMethod.POST, produces="application/json", consumes = "application/json")
@ResponseBody
public ResponseEntity newPost(@RequestBody String body) throws Exception {
ObjectMapper mapper = new ObjectMapper();
TypeReference<HashMap<String,Object>> typeRef = new TypeReference<HashMap<String,Object>>() {};
HashMap<String,Object> map = mapper.readValue(body, typeRef);
}
生成的HashMap
将使用ArrayList
作为标记列表:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.