Spring Boot - JSON Object Array to Java Array

17,971

Solution 1

You should probably create a Java class which represents the input JSON and use it in the method newPost(.....). For example:-

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.

Solution 2

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 Jackson's ObjectMapper along with TypeReference to get a map.

@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:

enter image description here

Solution 3

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.

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);
    }
}
Share:
17,971
decprog
Author by

decprog

Engineering Manager in the UK.

Updated on June 13, 2022

Comments

  • decprog
    decprog about 2 years

    I have an endpoint in spring boot that consumes this JSON as an example:

    {
        "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?

    Thanks.

    A mixutre of Ankur and Jose's answers solved this, thanks for the fast responses guys!