ResponseEntity不检索Integer

时间:2017-04-14 09:44:32

标签: spring spring-boot httpresponse

想要在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'的文件: enter image description here

在您回答之前:

  1. 在前端网站上一切正常
  2. 该错误位于后端站点

1 个答案:

答案 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