【问题标题】:Spring Boot - JSON Object Array to Java ArraySpring Boot - JSON 对象数组到 Java 数组
【发布时间】:2019-03-27 17:07:55
【问题描述】:

我在 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 {

我知道这里的问题是请求正文被保存为对象映射,这对于除 tagList 之外的所有其他属性都很好。如何让 tagList 成为 Java 中的字符串数组?

谢谢。

Ankur 和 Jose 的答案混合解决了这个问题,感谢你们的快速响应!

【问题讨论】:

  • 创建的是日期对象吗?
  • 对于我使用的类似场景,作为方法参数,自定义 pojo。
  • @SudhirOjha created 成为进入数据库之前的日期,但此时在应用程序中它可以是任何东西。
  • @cisk 而不是请求正文,只需将值直接存储到 POJO 中?
  • 是的。一个包含所有这些属性的 pojo,您可以在其中将 tagList 指定为 List。 Spring mvc 会自动将输入的 json 转换为 POJO。编辑:检查 Ankur Chrungoo 的第一个答案

标签: java arrays json spring-boot


【解决方案1】:

您可以为使用String[]List&lt;String&gt; 的请求创建自定义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);
    }
}

【讨论】:

    【解决方案2】:

    您可能应该创建一个表示输入 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 来改变字段名,只包含非空值,诸如此类。

    【讨论】:

      【解决方案3】:

      如果您不想使用自定义 POJO,您也可以自己处理反序列化为 Map。只需让您的控制器接受String,然后使用杰克逊的ObjectMapperTypeReference 来获取地图。

      @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 作为标签列表:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-02-22
        • 1970-01-01
        • 1970-01-01
        • 2020-02-11
        • 2019-08-10
        • 2019-05-03
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多