簡體   English   中英

如何從 Hibernate Validator 檢索默認驗證消息?

[英]How do I retrieve default validation messages from Hibernate Validator?

我正在嘗試使用MessageSource檢索默認驗證錯誤消息。 我正在使用的代碼使用反射來檢索message參數的值。 在不覆蓋message參數的約束上,我想檢索默認錯誤消息。 當我在驗證注釋上調用message方法時,我得到{org.hibernate.validator.constraints.NotBlank.message} (例如,對於@NotBlank注釋)。 然后我嘗試使用MessageSource來獲取錯誤消息,如下所示:

String message = messageSource.getMessage(key, null, Locale.US);

我嘗試將key設置為{org.hibernate.validator.constraints.NotBlank.message}org.hibernate.validator.constraints.NotBlank.message (移除大括號)甚至org.hibernate.validator.constraints.NotBlank但我一直得到null . 我在這里做錯了什么?

更新

澄清。 我的印象是 Spring 為其約束提供了一個默認的message.properties文件。 我在這個假設中正確嗎?

更新

更改問題的名稱以更好地反映我正在嘗試做的事情。

在遇到一個 Hibernate 人員的博客文章,並在 Hibernate Validator 源代碼中挖掘之后,我想我已經弄明白了:

public String getMessage(final Locale locale, final String key) {
    PlatformResourceBundleLocator bundleLocator = new PlatformResourceBundleLocator("org.hibernate.validator.ValidationMessages");
    ResourceBundle resourceBundle = bundleLocator.getResourceBundle(locale);

    try {
       final String k = key.replace("{", "").replace("}", "");
       return resourceBundle.getString(k);
    }
    catch (MissingResourceException e) {
       return key;
    }
}

因此,首先,您必須使用默認驗證消息實例化PlatformResourceBundleLocator 然后您從定位器中檢索一個ResourceBundle並使用它來獲取您的消息。 我不相信這會執行任何插值。 為此,您必須使用插值器; 我上面鏈接的博客文章對此進行了更詳細的介紹。

更新

另一種(更簡單的)方法是更新您的applicationContext.xml並執行以下操作:

<bean id="resourceBundleSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>org.hibernate.validator.ValidationMessages</value>
        </list>
    </property>
</bean>

現在您的MessageSource填充了默認消息,您可以執行messageSource.getMessage() 事實上,這可能是最好的方法。

彈簧靴 2.4.5

如果有人需要這個進行測試:

package some.package;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Locale;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.context.MessageSource;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

public class NotNullTest {

  private static MessageSource ms;
  private static String VAL_MSG_NOT_NULL;

  @BeforeAll
  static void setUpClass() {
    ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
    messageSource.setBasename("org/hibernate/validator/ValidationMessages");
    messageSource.setDefaultEncoding("UTF-8");
    ms = messageSource;
  }

  @BeforeEach 
  void setUp() {
    VAL_MSG_NOT_NULL = ms.getMessage("javax.validation.constraints.NotNull.message", null, Locale.ROOT);
    assertThat(VAL_MSG_NOT_NULL).isNotNull();
    assertThat(VAL_MSG_NOT_NULL).isNotBlank();
  }

  @Test
  void test() {
    assertThat(VAL_MSG_NOT_NULL).isEqualTo("must not be null");
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM