Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot - JSON Object Array to Java Array

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!

like image 794
decprog Avatar asked Oct 23 '18 12:10

decprog


People also ask

Can we convert JSON array to list in Java?

We can convert a JSON array to a list using the ObjectMapper class. It has a useful method readValue() which takes a JSON string and converts it to the object class specified in the second argument.

How do you convert JSON to Java object?

We can convert a JSON to Java Object using the readValue() method of ObjectMapper class, this method deserializes a JSON content from given JSON content String.

How do you write an array of objects in JSON?

A JSON array contains zero, one, or more ordered elements, separated by a comma. The JSON array is surrounded by square brackets [ ] . A JSON array is zero terminated, the first index of the array is zero (0). Therefore, the last index of the array is length - 1.


2 Answers

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.

like image 73
Ankur Chrungoo Avatar answered Sep 21 '22 12:09

Ankur Chrungoo


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

like image 27
Mike Avatar answered Sep 18 '22 12:09

Mike