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

标签 java spring microservices dto

我有 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/

相关文章:

java - 如何从文本字段获取 Long 值并将表单发送到 ArrayList

java - 如何在运行时动态地将外部 jar 文件添加到类路径中?

java - 对每个实体子类型使用不同的投影

java - 如何在Spring Boot中实现DataSourceAutoConfiguration

java - 如何从 startActivityForResult 读取结果

java - eclipse运行java程序时出现内部错误

java - MongoDB,写关注: need some clarification

spring - Maven 是像 Ant 一样的基于文本的工具吗?

docker - 如何使 Identityserver 重定向到我的网络应用程序?

docker - 在另一台主机上的 Netflix eureka 服务发现服务器中远程 dockerised spring boot 应用程序注册