ResponseEntity的返回值用法

我们在使用ResponseEntity时,更多的是为了设置不同的HttpResponse Code,如果你的系统偏好是通过Response Body中的Code来判断API状态即几乎所有API的HttpResponse Code=200,那么完全可以不使用ResponseEntity作为返回数据类型,只需要去返回Response Body,通过Body中开发者自定义的Code给API设置状态。

例如  Response Body
{
    code: 200,
    ErrorMessage: "error message",
    data: {}
}

Get请求时

return ResponseEntity.ok();
例如:
@GetMapping({"findAll"})
    public ResponseEntity<PageResult<User>> findAll(@RequestParam(name = "admin", required = false) String admin, @RequestParam(name = "page", defaultValue = "1") Integer page, @RequestParam(name = "rows", defaultValue = "10") Integer rows) {
        PageResult<User> userPageResult = this.userService.findAll(admin, page, rows);
        return ResponseEntity.ok(userPageResult);
    }

Post请求新增一条记录时,有返回值

//body()里存放的是返回的内容
return ResponseEntity.status(HttpStatus.CREATE).body();
例如:
@PostMapping("save")
   public ResponseEntity save(@RequestBody User user) throws Exception {
        return ResponseEntity.status(HttpStatus.CREATED).body(this.userService.save(user));
   }

Post请求新增一条记录时,无返回值

return new ResponseEntity(HttpStatus.CREATED);

Delete删除请求,无返回

return new ResponseEntity(HttpStatus.NO_CONTENT);
例如:
@DeleteMapping({"delete"})
    public ResponseEntity delete(@RequestParam(name = "ids") Integer[] ids) {
        return ResponseEntity.ok(this.userService.delete(ids));
    }

Put更新请求,无返回值

ResponseEntity.noContent().build();
例如:
@PutMapping({"update"})
    public ResponseEntity update(@RequestBody User user) throws Exception {
        return ResponseEntity.ok(this.userService.update(user));
    }
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
使用ResponseEntity自定义Headers的方法如下: 首先,创建一个HttpHeaders对象来设置自定义的Headers。可以使用add()方法来添加具体的Header键值对。例如,可以使用set()方法来设置一个键值对。 然后,创建一个ResponseEntity对象并将要返回的响应体和自定义的Headers传入。可以使用ok()方法来创建一个成功的ResponseEntity对象。 最后,在Controller方法中,将ResponseEntity对象作为返回值返回即可。 下面是一个示例代码: ```java import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @GetMapping("/customHeaders") public ResponseEntity<String> getResponseWithCustomHeaders() { // 创建自定义的Headers HttpHeaders headers = new HttpHeaders(); headers.add("Custom-Header", "Custom Value"); headers.set("Another-Header", "Another Value"); // 创建ResponseEntity对象并设置自定义Headers ResponseEntity<String> responseEntity = ResponseEntity.ok() .headers(headers) .body("Response Body"); return responseEntity; } } ``` 在上述示例中,通过访问`/customHeaders`路径,将返回一个带有自定义Headers的ResponseEntity对象。其中,自定义Headers包括"Custom-Header"和"Another-Header"两个键值对。 请注意,在使用ResponseEntity时,需要在Controller方法中返回ResponseEntity对象,而不是直接返回响应体内容。通过设置ResponseEntity对象的headers属性来设置自定义Headers。在Spring框架中,可以使用ResponseEntity对象来设置响应头信息。 : 需要注意的是,在使用ResponseEntity时,需要在Controller方法中返回ResponseEntity对象,而不是直接返回响应体内容。 : 在Spring框架中,可以使用ResponseEntity对象来设置响应头信息。可以通过以下代码设置响应头:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值