【问题标题】:Spring Data - Using property values in projectionsSpring Data - 在投影中使用属性值
【发布时间】:2019-11-15 13:13:31
【问题描述】:

我有 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 值?

【问题讨论】:

  • @Value("#{${app.url}/pictures/#{target.imageId}}") 或类似的东西。 baeldung.com/spring-expression-language
  • @AlanHay 我试过@Value("#{ ${app.url} + '/pictures/' + target.imageId}"),不幸的是它没有用。然后它返回一个EL1041E: After parsing a valid expression, there is still more data in the expression: 'lcurly({)' 错误。
  • 也许可以直接访问systemProperties @Value("#{ systemProperties['app.url'] + '/pictures/' + target.imageId }")?
  • @MartinBG 显然application.properties 值不在systemProperties 中,或者您的示例中出现了其他问题。 Property or field 'systemProperties' cannot be found on object of type 'org.springframework.data.projection.SpelEvaluatingMethodInterceptor$TargetWrapper' - maybe not public or not valid?

标签: spring spring-boot spring-data projection


【解决方案1】:

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

例子:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-05
    • 2015-02-05
    • 2018-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-19
    相关资源
    最近更新 更多