相关疑难解决方法(0)

Spring Boot 将验证代码映射到 MessageSource 消息

问题:

我正在尝试使用尽可能多的 Spring Boot Auto-configuration 来减少样板代码。我无法让 Spring 验证代码自动映射到我的外部化 messages.properties。只有当我添加自己的LocalValidatorFactoryBean并使用完全限定的 javax 约束消息时,它才会起作用。

目标

我想重写defaultMessage为 @ javax.validation.constraints.NotNull全球在我的应用程序基于Spring输入验证码秒。

我做的第一件事就是注册这个 bean...我真的必须注册吗?我看到 Spring Boot 有一个,但它似乎与 messages.properties 无关。

@Bean
public LocalValidatorFactoryBean validator(MessageSource messageSource) {
  LocalValidatorFactoryBean bean = new LocalValidatorFactoryBean();
  bean.setValidationMessageSource(messageSource);
  return bean;
}
Run Code Online (Sandbox Code Playgroud)

下一个让我很惊讶。Spring Validation 有这个漂亮的DefaultMessageCodesResolver,它为看起来像这样的失败提供了出色的代码策略:

"code": "NotNull",
"codes": [
   "NotNull.user.firstName",
   "NotNull.firstName",
   "NotNull.java.lang.String",
   "NotNull"
],
Run Code Online (Sandbox Code Playgroud)

但是这些代码甚至不考虑用于@NotNull 约束。验证转换的 spring 文档暗示这样的东西确实存在,但还没有任何有用的东西。

我在这里编写了一个示例应用程序,您可以看到它不起作用。

题

如何根据messages.properties 中的代码覆盖Spring Validation 错误的默认消息?

相关的build.gradle

  • springBootVersion = '2.0.0.RELEASE'
  • 编译('org.springframework.boot:spring-boot-starter-web')

src/main/resources/messages.properties:

NotNull.user.firstName=This doesn't work
NotNull.firstName=Or …
Run Code Online (Sandbox Code Playgroud)

spring spring-mvc spring-boot spring-web

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

Spring Boot Starter Cache - 缺少Velocity?

我正在使用ehcache测试Spring Boot 1.3.0.M3并遇到一些问题.

当我启动应用程序后,控制台上发生异常:

线程"main"中的异常org.springframework.beans.factory.BeanCreationException:在类路径资源中定义名为'velocityConfigurer'的bean时出错[org/springframework/boot/autoconfigure/velocity/VelocityAutoConfiguration $ VelocityWebConfiguration.class]:init的调用方法失败; 嵌套异常是org.springframework.beans.factory中的org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)中的java.lang.NoClassDefFoundError:org/apache/velocity/runtime/log/CommonsLogLogChute.在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory上的support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545).

我的pom.xml:

...
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.0.M3</version>
</parent>
...
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-cache</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    </dependency>
    ...
</dependencies>
...
Run Code Online (Sandbox Code Playgroud)

mvn依赖:树

org.springframework.boot:spring-boot-starter-web:jar:1.3.0.M3:compile [INFO] | + - org.springframework.boot:spring-boot-starter:jar:1.3.0.M3:compile [INFO] | | + - org.springframework.boot:spring-boot:jar:1.3.0.M3:compile [INFO] | | + - org.springframework.boot:spring-boot-autoconfigure:jar:1.3.0.M3:compile [INFO] | | - org.yaml:snakeyaml:jar:1.15:编译[INFO] | + - org.springframework.boot:spring-boot-starter-validation:jar:1.3.0.M3:compile [INFO] | | - org.hibernate:hibernate-validator:jar:5.2.1.Final:compile [INFO] | | + - javax.validation:validation-api:jar:1.1.0.Final:compile [INFO] | | - com.fasterxml:classmate:jar:1.1.0:compile …

java spring maven

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

标签 统计

spring ×2

java ×1

maven ×1

spring-boot ×1

spring-mvc ×1

spring-web ×1