Spring Boot - JSON 对象数组到 Java 数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52949425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot - JSON Object Array to Java Array
提问by decprog
I have an endpoint in spring boot that consumes this JSON as an example:
我在 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"]
}
The endpoint:
终点:
@RequestMapping(value="/newPost", method = RequestMethod.POST, produces="application/json", consumes = "application/json")
@ResponseBody
public ResponseEntity newPost(@RequestBody Map<String, Object> body) throws Exception {
I know the issue here is the Request body is being saved as a Map of objects which is fine for all the other attributes except the tagList. How can I get tagList to be an array of Strings in Java?
我知道这里的问题是请求正文被保存为对象映射,这对于除 tagList 之外的所有其他属性都很好。如何让 tagList 成为 Java 中的字符串数组?
Thanks.
谢谢。
A mixutre of Ankur and Jose's answers solved this, thanks for the fast responses guys!
Ankur 和 Jose 的答案的混合解决了这个问题,感谢快速响应的家伙!
采纳答案by Ankur Chrungoo
You should probably create a Java class which represents the input JSON and use it in the method newPost(.....)
. For example:-
您可能应该创建一个表示输入 JSON 的 Java 类并在方法中使用它newPost(.....)
。例如:-
public class UserPostInfo {
private int userId;
private String postBody;
private String postTitle;
private Date created;
private List<String> tagList;
}
Also, include the getter/setter methods in this class. If you want to modify the behavior of JSON parsing, you can use Annotations to change field names, include only non-null values, and stuff like this.
此外,在此类中包括 getter/setter 方法。如果您想修改 JSON 解析的行为,您可以使用 Annotations 来更改字段名称,仅包含非空值等。
回答by Jose Martinez
You can create a custom Java POJO for the request that uses String[]
versus List<String>
. Here I did it for you using the site jsonschema2pojo.
您可以为使用String[]
vs的请求创建自定义 Java POJO List<String>
。在这里,我使用站点jsonschema2pojo为您完成了这项工作。
package com.stackoverflow.question;
import com.fasterxml.Hymanson.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);
}
}
回答by Mike
If you don't want to use a custom POJO you could also just handle the deserialization into a Map yourself. Just have your controller accept a String
and then use Hymanson's ObjectMapper
along with TypeReference
to get a map.
如果您不想使用自定义 POJO,您也可以自己将反序列化处理为 Map。只需让您的控制器接受 aString
然后使用 Hymanson's ObjectMapper
withTypeReference
来获取地图。
@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);
}
The resulting HashMap
will use an ArrayList
for the tag list:
结果HashMap
将使用ArrayList
标记列表: