我正在 Spring Boot 项目中使用
ResponseEntity<>
,我想知道在调用 getBody()
之前是否必须检查 responseBody 对象是否为 null?
例如:
ResponseEntity<Customer> responseBody = customerClient.getCustomer(id);
我不确定
ResponseEntity
对象是否可以为空。我可以直接执行 Customer customer = responseBody.getBody();
还是应该在此之前检查 null ?
Spring 被设计为始终在responseBody 中返回一个值。可以为 null 的是您在 responseBody 上调用的 getBody() 方法。
您可能想看看以下内容: