Java将变量传递到映射的DTO方法吗?

问题描述 投票:1回答:1

我有Spring Boot Application,其实现包含具有以下功能的方法。该实现使用2个DTO与之绑定数据。有什么合适的方法可以将值从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;
    }
java spring microservices dto
1个回答
0
投票

您总是可以将更多参数传递给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) ...

© www.soinside.com 2019 - 2024. All rights reserved.