我应该在 Spring Boot 中检查 ResponseEntity<> 对象是否为 null 吗?

问题描述 投票:0回答:1

我正在 Spring Boot 项目中使用

ResponseEntity<>
,我想知道在调用
getBody()
之前是否必须检查 responseBody 对象是否为 null?

例如:

ResponseEntity<Customer> responseBody = customerClient.getCustomer(id);

我不确定

ResponseEntity
对象是否可以为空。我可以直接执行
Customer customer = responseBody.getBody();
还是应该在此之前检查 null ?

java spring spring-boot nullpointerexception response-entity
1个回答
0
投票

Spring 被设计为始终在responseBody 中返回一个值。可以为 null 的是您在 responseBody 上调用的 getBody() 方法。

您可能想看看以下内容:

© www.soinside.com 2019 - 2024. All rights reserved.