Spring数据-在投影 中使用属性值

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

我有Spring Data Rest Application,在其中创建了这样的投影

@Projection(name = "UserWithAvatar", types = { User.class })
public interface UserWithAvatar {
    String getName();
    String getEmail();

    @Value("${app.url}/pictures/#{target.imageId}")
    String getAvatar();
}

非工作部分是getAvatar,它生成用于查看图片的URL。但是,此${app.url}在投影内部不起作用。如何在其中添加此application.properties值?

spring spring-boot spring-data projection
1个回答
0
投票

@environment.getProperty('app.url')块中使用#{}。它适用于Spring Boot 2.3.0,我不确定较旧的版本。

示例:

@Value("#{target.pictureUrl ?: @environment.getProperty('app.url') + 'static/default-avatar.png'}")
String getAvatarUrl();
© www.soinside.com 2019 - 2024. All rights reserved.