问题
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
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)
Implementation
@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;
}
回答1:
You can always pass more parameters to a lambda expression
searchProfileData = searchData.map(x -> this.convertProfileToProfileCreditDTO(x, JAY));
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) ...
来源:https://stackoverflow.com/questions/59304182/java-pass-variable-into-mapped-dto-method