相关疑难解决方法(0)

Spring Boot验证消息未解析

我无法解析验证消息.

我一直在搜索和阅读网络和SO几个小时,我想将问题与自定义弹簧验证错误的明确答案联系起来

我确实有一个MessageSourcebean定义并且messages.properties正确读取,因为我也使用它来显示常规文本th:text="#{some.prop.name},这确实很好用.只是验证错误不会以它应该的方式工作.我确定这是一个我忽略的愚蠢错误......验证本身工作正常.

约束:

@NotEmpty(message="{validation.mail.notEmpty}")
@Email()
private String mail;
Run Code Online (Sandbox Code Playgroud)

messages.properties:

# Validation
validation.mail.notEmpty=The mail must not be empty!
Run Code Online (Sandbox Code Playgroud)

模板部分:

<span th:if="${#fields.hasErrors('mail')}" th:errors="*{mail}"></span>
Run Code Online (Sandbox Code Playgroud)

显示的文字:

{validation.mail.notEmpty}
Run Code Online (Sandbox Code Playgroud)

我尝试了很多变化,都没有成功.

@NotEmpty(message="validation.mail.notEmpty")
@NotEmpty(message="#{validation.mail.notEmpty}")
Run Code Online (Sandbox Code Playgroud)

只显示消息字符串的确切值,无需解析.

<span th:if="${#fields.hasErrors('mail')}" th:errors="${mail}"></span>
<span th:if="${#fields.hasErrors('mail')}" th:errors="#{mail}"></span>
<span th:if="${#fields.hasErrors('mail')}" th:errors="#{*{mail}}"></span>
<span th:if="${#fields.hasErrors('mail')}" th:errors="#{__*{mail}__}"></span>
Run Code Online (Sandbox Code Playgroud)

会导致错误.


编辑:

经过调试,我偶然发现了这个问题:

类: org.springframework.context.support.MessageSourceSupport

方法: formatMessage(String msg, Object[] args, Locale locale)

将被召唤

formatMessage("{validation.mail.notEmpty}", null, locale /*German Locale*/)

它会遇到 if (messageFormat == INVALID_MESSAGE_FORMAT) {

所以...我的消息格式不正确.这超出了我的范围/知识.谁知道这意味着什么?

java spring thymeleaf spring-boot

17
推荐指数
3
解决办法
2万
查看次数

使用Spring的Validator界面进行Bean验证中的自定义消息

我正在将Spring Boot 1.3.5与Rest Controllers一起使用,并且一切正常。

我还在官方文档中使用了Spring的验证示例技术(JSR-303 Bean验证API和Spring的验证器接口,我都尝试过并且遇到相同的问题),并且验证有效,但是我无法配置自定义消息。

我已经配置了messages.properties文件,并且可以很好地访问此文件上的消息。但是,此验证似乎无法读取或访问通过Spring Boot自动配置的我的消息源(messages.properties)。

我可以直接通过@Autowired从控制器中注入的消息源对象访问消息(代码中有注释)。但是,Spring的验证器接口或JSR-303 Bean验证的绑定结果似乎无法访问MessageSource中加载的messages.properties。我得到的结果是我的错误有代码,但没有默认消息。

这是我的应用程序类:

@SpringBootApplication
@ImportResource({ "classpath:security/cas-context.xml", "classpath:security/cas-integration.xml",
    "classpath:security/security.xml" })
@EnableAutoConfiguration(exclude = VelocityAutoConfiguration.class) // http://stackoverflow.com/questions/32067759/spring-boot-starter-cache-velocity-is-missing

public class Application extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(Application.class);
}

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

@Bean
public ServletRegistrationBean cxfServlet() {
    return new ServletRegistrationBean(new CXFServlet(), "/services/*");
}

@Bean(name = Bus.DEFAULT_BUS_ID)
public SpringBus springBus() {
    return new SpringBus();
}

@Bean
public Nfse nfseService() {
    return new NfseImpl();
}

@Bean
public Endpoint …
Run Code Online (Sandbox Code Playgroud)

java spring bean-validation spring-validator spring-boot

1
推荐指数
1
解决办法
9722
查看次数