@ResponseBody
返回
[{"id":1010,"name":"projectname2"}]
输入 json 字符串
但我需要一个以下 json 字符串
[{"id":1010,"name":"projectname2","age":"21"}]
那么如何将年龄属性连接到默认生成的 json 字符串?
我使用带有 spring-json jar 的 java spring-mvc 框架
@RequestMapping(value = "/projectsByEmployeeId/list", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<Project> getProjectsByEmployeeId(
HttpServletRequest request) {
String filter = request.getParameter("filter");
FilterAttributes[] filterAttributes = null;
try {
filterAttributes = new ObjectMapper().readValue(filter,
FilterAttributes[].class);
} catch (Exception exception) {
logger.error("Filtering parameter JSON string passing failed",
exception);
}
if ((filterAttributes != null)
&& (!filterAttributes[0].getStringValue().isEmpty())) {
return utilityService.getProjectsByEmployeeId(44L);//this is an example id
} else {
return utilityService.getProjects();
}
}