With the below code I have converted list to json but the format is as follows:
{\"GodownMaster\":[{\"pname\":\"FCI CHARLAPALLI\",\"pcode\":\"16042\"},
Here is what I have used:
@RequestMapping("/alluserreportJSON")
public @ResponseBody String getusersJSON() {
ObjectMapper objectMapper = new ObjectMapper();
//Set pretty printing of json
objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
List userlist = null;
@SuppressWarnings("unused")
String exception = null;
String arrayToJson = null;
try {
userlist = userService.findAllUsers();
arrayToJson = objectMapper.writeValueAsString(userlist);
} catch (Exception ex) {
ex.printStackTrace();
exception = ex.getMessage();
}
return arrayToJson;
}
Hope it helps someone. You can see it working here.