6

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!

5
  • Is created is date object ? Commented Oct 23, 2018 at 12:46
  • For a similar scenario I used, as method parameter, a custom pojo. Commented Oct 23, 2018 at 12:47
  • @SudhirOjha created becomes a date before entry into the DB, but at this point in the app it could be anything. Commented Oct 23, 2018 at 12:48
  • @cisk instead of a request body, just store the values straight into a POJO? Commented Oct 23, 2018 at 12:49
  • Yes. A pojo containing all those properties, and in there you can specify your tagList as List<String>. Spring mvc will convert the input json to the POJO automatically. Edit: check the first answer by Ankur Chrungoo Commented Oct 23, 2018 at 12:52

3 Answers 3

6

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.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

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);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.