Java传递变量到映射的DTO方法? [英] Java pass variable into mapped DTO method?
问题描述
我有Spring Boot Application,其实现包含具有以下功能的方法.该实现使用2个DTO与之绑定数据.有什么合适的方法可以将值从 JAY 传递到硬编码" 10.00 "的值吗?我的主要问题是 this :: convertProfileToProfileCreditDTO ,是否可以在此表达式中传递参数?
我已经使用 Java DTO对象搜索机制来进行渗透
I have Spring Boot Application with implementation containing methods with following functions. The implementation uses 2 DTO's to bind data with. Is there an appropriate way how I could pass value from JAY to value where '10.00' is hardcoded? I have main issue with 'this::convertProfileToProfileCreditDTO' is it possible to pass a parameter in this expression?
I have used Java DTO Object search mechanism? for insipration
如果我尝试在以下代码中添加一个参数,该代码将被以下代码截断:: convertProfileToProfileCreditDTO抱怨返回类型错误
If I try to add a parameter within code snipped below the this::convertProfileToProfileCreditDTO complains about bad return type
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表达式
You can always pass more parameters to a lambda expression
searchProfileData = searchData.map(x -> this.convertProfileToProfileCreditDTO(x, JAY));
请注意,如果要使用this::
样式使函数调用保持简单,则可以创建一个数据对象以携带所需的参数
As a side note, if you want to keep the functional call simple with this::
style, you can create a data object to carry in your required parameters
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方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!