我有 Spring Boot 应用程序,其实现包含具有以下功能的方法。该实现使用 2 个 DTO 来绑定(bind)数据。是否有适当的方法可以将值从 JAY 传递到硬编码的“10.00”值?我对“this::convertProfileToProfileCreditDTO”有主要问题,是否可以在此表达式中传递参数?
我用过Java DTO Object search mechanism?灵感
如果我尝试在 this::convertProfileToProfileCreditDTO 下面的代码片段中添加参数,则会提示返回类型错误
convertProfileToProfileCreditDTO(final Profile theProfile, Double JAY)
实现
@Override
public Double testThisParam(Double profileCredit) {
Double JAY = profileCredit;
log.error(String.valueOf(JAY));
return JAY;
}
@Override
public Page<ProfileCreditDTO> findProfileBySelectedParameters(String username, Pageable pageable) {
Page<Profile> searchData= profileRepository.findByAllParameters(username, pageable);
Page<ProfileCreditDTO> searchProfileData=null;
if(searchData != null)
searchProfileData=searchData.map(this::convertProfileToProfileCreditDTO);
return searchProfileData;
}
public ProfileCreditDTO convertProfileToProfileCreditDTO(final Profile theProfile ){
if(theProfile == null)
return null;
ProfileCreditDTO theDTO= new ProfileCreditDTO();
theDTO.setProfile(theProfile);
CreditDTO theCreditDto = profileCreditClient.findClientByProfileId(theProfile.getId(), 10.00);
if(theCreditDto != null )
theDTO.setCredit(theCreditDto);
else {
return null;
}
return theDTO;
}
最佳答案
您始终可以向 lambda 表达式传递更多参数
searchProfileData = searchData.map(x -> this.convertProfileToProfileCreditDTO(x, JAY));
顺便说一句,如果您想使用 this::
风格保持函数调用简单,您可以创建一个数据对象来携带所需的参数
class MyObject {
Profile theProfile;
Double JAY;
// public constructor with parameters
}
// then construct and use this
MyObject o = new MyObject(theProfile, testThisParam(...));
// and then change parameter of target method
convertProfileToProfileCreditDTO(MyObject myObject) ...
关于Java 将变量传递给映射的 DTO 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59304182/