我在Controller中有一个简单的方法
@RequestMapping("admin")
public @ResponseBody
Boolean admin() {
Boolean success = true;
return success;
}
作为回应,我想返回 { "success": true }
Annontation@ResponseBody
表示响应将为JSON。但是现在我的回答是正确的。
还有其他解决方法吗?
或者我应该做类似的事情
@RequestMapping("admin")
public @ResponseBody
Map<String, Boolean> admin() {
Map<String, Boolean> success = new TreeMap<String, Boolean>();
success.put("success", true);
return success;
}
我想知道最佳做法。
您不能返回原始类型(或原始包装类型)并获取JSON对象作为响应。您必须返回一些对象,例如Map
或自定义域对象。
您的问题中显示的Map方法完全有效。如果需要,可以使用将其压缩为一个不错的单行代码Collections.singletonMap()
。
@RequestMapping
@ResponseBody
public Map<String, Boolean> admin() {
return Collections.singletonMap("success", true);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句