我想执行空检查,因为在这个映射器类的实现过程中,如果值为空,它将抛出异常。
@Mapping(target = "rotorSpeed", expression = "java(MethodU.changeValues(BigDecimal.valueOf(motorBike.getValue1().getValue2()),motorBikeEntity.getOption1().getOption2()))")
----Method
public class MethodU {
public static BigDecimal changeValues(BigDecimal value1 , Double value2) {
return (value1.multiply(BigDecimal.valueOf(value2))).setScale(0, RoundingMode.HALF_UP);
}
我们如何检查 motorBike.getValue1() !=null && motorBike.getValue1().getValue2() !=null 的值,对于我们传递表达式的所有对象都是类似的。
我尝试了 Objects.nonNull() ,它不起作用,因为它返回布尔值。即使我尝试使用 Objects.requireNonNull() 也不起作用。
如果对象为空,是否可以设置默认值?
正如多次提到的,您的选择是
Optional<Int>
值来返回空值。