想要在Integer
中返回简单的ResponseEntity
:
@PreAuthorize("hasAnyAuthority('WORKER')")
@RequestMapping(value = "/countFiles", method = RequestMethod.GET)
@ResponseBody
public ResponseEntity<Integer> countFiles(HttpServletRequest request){
Integer count = fileService.countFiles(request);
if(count == null){
return new ResponseEntity<Integer>(HttpStatus.BAD_REQUEST);
}
return new ResponseEntity<Integer>(count, HttpStatus.OK);
}
当我这样做时,在前端网站上我没有提交名为'count'的文件:
在您回答之前:
答案 0 :(得分:0)
如果你想要一个名为&#34; count&#34;的字段,你需要在对象或地图结果中包含字段名称。
@PreAuthorize("hasAnyAuthority('WORKER')")
@RequestMapping(value = "/countFiles", method = RequestMethod.GET)
public ResponseEntity<Integer> countFiles(HttpServletRequest request)
{
Integer count = fileService.countFiles(request);
if (count == null) {
return ResponseEntity.badRequest().build();
}
return ResponseEntity.ok(Collections.singletonMap("count", count));
}
仅供参考,使用静态ResponseEntity.*
方法创建新的ResponseEntity
实例可能更容易。此外,如果返回值为@ResponseBody
,则您不需要ResponseEntity
。