Java pass variable into mapped DTO method?

核能气质少年 提交于 2020-01-25 01:05:23

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!