如何在java中检查setter方法内部的对象空检查?

如何在java中检查setter方法内部的对象空检查?,java,Java,这是通常的方式 productMenu = productMenuService.getProductMenuById(Integer.parseInt(request.getString("productMenuId"))); if (productMenu != null) { productPost.setProductMenu(productMenu); } 我可以在setter方法中进行非空对象检查吗 p

这是通常的方式

productMenu = productMenuService.getProductMenuById(Integer.parseInt(request.getString("productMenuId")));
            if (productMenu != null) {
                productPost.setProductMenu(productMenu);
            }
我可以在setter方法中进行非空对象检查吗

productPost.setProductMenu(productMenuService.getProductMenuById(Integer.parseInt(request.getString("productMenuId"))));

试图减少代码。非常感谢您的帮助。

版本>=Java 8 您可以使用Optional来确保只设置非null值

 Optional.ofNullable(request.getString("productMenuId"))
 .map(value-> Integer.parseInt(value))
 .map(productMenuService::getProductMenuById);
 .ifPresent(productPost::setProductName);
版本

void setProductMenu(*ProductMenuType* productMenu) {
  // do all your checking before setting here
  if (productMenu != null) {
     productPost.setProductMenu(productMenu);
  }
}
但是您的情况没有意义(没有其他部分),因为如果我们没有为变量设置值,那么默认情况下它将是
NULL

if(productMenuService.getProductMenuById(Integer.parseInt(request.getString(“productMenuId”)==NULL){
if(productMenuService.getProductMenuById(Integer.parseInt(request.getString("productMenuId")) == null){
   return new ResponseEntity<String>("Not found", HttpStatus.BAD_REQUEST);
}else{
productMenu = productMenuService.getProductMenuById(Integer.parseInt(request.getString("productMenuId")));

}
//in your repository first check that data is present or not like this
repository.findById(id).isPresent(){
return repository.getOne(id);
}else return null
返回新的响应属性(“未找到”,HttpStatus.BAD_请求); }否则{ productMenu=productMenuService.getProductMenuById(Integer.parseInt(request.getString(“productMenuId”)); } //在存储库中,首先检查数据是否存在 repository.findById(id).isPresent(){ 返回repository.getOne(id); }否则返回空值
引发异常

如果有人将无效的产品菜单ID传递给您的应用程序,则表明他们犯了错误。您不应该表现得好像他们没有做错什么。您的应用程序当然不应该保留旧的产品菜单,而完全忽略用户的请求

if (productMenu == null) {
    throw new RuntimeException("Invalid product menu ID.");
}

productPost.setProductMenu(productMenu);
虽然这不会使代码更短,但会使代码更好

一个健壮的应用程序不会在实际上不工作的情况下假装它在工作。否则会让用户感到沮丧,并且会使调试变得更加困难,因为问题可能需要数周、数月或数年才能被发现

理想情况下,您将希望创建自己的异常类,并抛出该异常类:

if (productMenu == null) {
    throw new InvalidProductMenuIDException("Invalid product menu ID.");
}

productPost.setProductMenu(productMenu);
其中异常类仅为:

public class InvalidProductMenuIDException
extends Exception {
    public InvalidProductMenuIDException(String message) {
        super(message);
    }

    public InvalidProductMenuIDException(Throwable cause) {
        super(cause);
    }

    public InvalidProductMenuIDException(String message,
                                         Throwable cause) {
        super(message, cause);
    }
}

为什么IF块中的代码不只是代码>产品TestPoest.StudioToMtCudio(产品菜单);?考虑一下,我想检索对象并在NULL中进行NULL检查,如果我个人的话,我宁愿分离单个步骤La<代码>可选。OnnulLable(请求.GETSHIPE(“产品TMeMuuID”)).map(整型::PARSETIN)。(productMenuService::getProductMenuById).ifPresent(productPost::setProductMenu)@daniu,这不仅是看起来更干净的代码(如果在每个.map之前插入换行符),而且更健壮,因为在case
request.getString(“productMenuId”)中,您不会面临被
Integer.parseInt
抛出异常的风险
为空
可选
不应被用作
的替代品,如果它取决于具体情况。
@VFR。对于创建新对象,可选是好的。它不适合更新。这里可选等于给定的代码。您为什么假设
,因为产品id在OP中无效usecase@CoderinoJavarino因为一个空的产品表示提供的产品ID无效。我很难想象允许浏览器或其他客户端提供实际不是现有产品ID的产品ID的有效用例。