Search code examples
springspring-bootspring-dataprojection

Spring Data - Using property values in projections


I have Spring Data Rest Application where I created a projection like this

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

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

The non working part is the getAvatar, it generates a url for seeing the picture.
However this ${app.url} is not working inside the projection.
How would I add this application.properties value in there?


Solution

  • Use @environment.getProperty('app.url') inside #{} block. It works on Spring Boot 2.3.0, i'm not sure about older versions.

    Example:

    @Value("#{target.pictureUrl ?: @environment.getProperty('app.url') + 'static/default-avatar.png'}")
    String getAvatarUrl();