I am running into a small trouble here with a Spring Boot Application. As per my understanding, Jackson dependency is auto configured with the spring-boot-starter-web and serialize/deserialize objects into json for the classes annotated as @RestController.
The issue is it is not having the same behavior as expected instead it is returning the response back in plain/text format.
Below are the code snippet that i am trying to execute.
@GetMapping(value = "/user/",produces = "application/JSON")
public List<String> getUser(){
List<String> newUsers = new ArrayList<String>();
newUsers.add("User 1");
newUsers.add("User 2");
newUsers.add("User 3");
return newUsers;
}
Response: Can someone please tell me what exactly i am doing wrong here?
Thanks J
Yup , just a JSON array is also a valid JSON . If you want to output the JSON in the object form like :
{
"users":["User 1","User 2","User 3"]
}
You can do :
@GetMapping(value = "/user/",produces = "application/JSON")
public Map<String,Object> getUser(){
Map<String,Object> result = new HashMap<String,Object>();
List<String> newUsers = new ArrayList<String>();
newUsers.add("User 1");
newUsers.add("User 2");
newUsers.add("User 3");
result.put("users" , newUsers);
return result;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.