Spring-Boot-20-ResponseEntity

Spring ResponseEntity

  1. ResponseEntity处理http响应

1. ResponseEntity

  1. 标识了整个http响应:状态码、头部信息以及相应体内容
  2. 如果要使用ReponseEntity,必须在请求点返回,通常在spring rest中实现。

1.1. 内嵌接口

  1. HeadersBuilder:不能设置任何响应体属性
  2. BodyBuilder(是上面的子接口)
@GetMapping("/hello")
ResponseEntity<String> hello() {
    return ResponseEntity.ok("Hello World!");
}

1.2. 常见相应响应

  1. BodyBuilder accepted();
  2. BodyBuilder badRequest();
  3. BodyBuilder created(java.net.URI location);
  4. HeadersBuilder<?> noContent();
  5. HeadersBuilder<?> notFound();
  6. BodyBuilder ok();
  7. BodyBuilder status(HttpStatus status)
  8. BodyBuilder status(int status)

1.2.1. status()的使用

@GetMapping("/age")
ResponseEntity<String> age(@RequestParam("yearOfBirth") int yearOfBirth) {
    if (isInFuture(yearOfBirth)) {
        return ResponseEntity.badRequest()
            .body("Year of birth cannot be in the future");
    }

    return ResponseEntity.status(HttpStatus.OK)
        .body("Your age is " + calculateAge(yearOfBirth));
}

1.3. 简单使用

@GetMapping("/age")
ResponseEntity<String> age(@RequestParam("yearOfBirth") int yearOfBirth) {
    if (isInFuture(yearOfBirth)) {
        return new ResponseEntity<>("Year of birth cannot be in the future", HttpStatus.BAD_REQUEST);
    }
    return new ResponseEntity<>("Your age is " + calculateAge(yearOfBirth), HttpStatus.OK);
}

1.4. 设置Http响应头

//方式一
@GetMapping("/customHeader")
ResponseEntity<String> customHeader() {
    HttpHeaders headers = new HttpHeaders();
    headers.add("Custom-Header", "foo");
    return new ResponseEntity<>("Custom header set", headers, HttpStatus.OK);
}
//方式二
@GetMapping("/customHeader")
ResponseEntity<String> customHeader() {
    return ResponseEntity.ok()
        .header("Custom-Header", "foo")
        .body("Custom header set");
}
  1. .body()返回的是ResponseEntity,而并不是BodyBuilder,需要在最后进行调用。
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值