Java Spring启动-Hibernate验证在Tomcat和Jetty中的行为不同
我已经开发了许多Spring Boot应用程序(使用旧版本1.5.21.RELEASE),我一直使用Tomcat,因为它是Spring Boot中的默认版本 现在我需要切换到Jetty,这不是什么主要任务,只是排除了Tomcat并添加了Jetty依赖项。到目前为止还不错 当我第一次使用干净的数据库运行它时,由于Java Spring启动-Hibernate验证在Tomcat和Jetty中的行为不同,java,spring-boot,hibernate,tomcat,jetty,Java,Spring Boot,Hibernate,Tomcat,Jetty,我已经开发了许多Spring Boot应用程序(使用旧版本1.5.21.RELEASE),我一直使用Tomcat,因为它是Spring Boot中的默认版本 现在我需要切换到Jetty,这不是什么主要任务,只是排除了Tomcat并添加了Jetty依赖项。到目前为止还不错 当我第一次使用干净的数据库运行它时,由于约束冲突,它失败了,查看它是我在用户实体上设置的自定义验证消息。这是我的实体: @实体 @表(name=“users”) @资料 公共类用户{ @身份证 @列(name=“username
约束冲突
,它失败了,查看它是我在用户
实体上设置的自定义验证消息。这是我的实体:
@实体
@表(name=“users”)
@资料
公共类用户{
@身份证
@列(name=“username”)
@NotEmpty(message=“*请提供您的用户名”)
@JsonView(DataTablesOutput.View.class)
私有字符串用户名;
@列(name=“password”)
@长度(最小值=5,message=“*您的密码必须至少包含5个字符”)
@NotEmpty(message=“*请提供您的密码”)
@JsonProperty(access=JsonProperty.access.WRITE_ONLY)
私有字符串密码;
@列(name=“email”)
@电子邮件(message=“*请提供有效电子邮件”)
@NotEmpty(message=“*请提供电子邮件”)
@JsonView(DataTablesOutput.View.class)
私人字符串电子邮件;
@列(name=“电话”)
@NotEmpty(message=“*请提供电话号码”)
专用串电话;
}
在用户表中,电话
列允许空值。实体中使用的验证是针对Web CRUD控制器的,在@Controller
端点的@RequestBody
之前有一个@Valid
,还有一个BindingResult
,这对Tomcat和Jetty都有好处
然后,当应用程序启动时,我创建一个defaultUser,为其所有属性设置默认值,电话号码除外,该号码保持为空
对我来说奇怪的是,Tomcat并没有失败,但Jetty却失败了。我是说,我有这样一句话:
userRepository.save(defaultUser);
如上所述,defaultUser的电话号码为空。由于@NotEmpty注释是org.hibernate.validator.constraints.NotEmpty
的一部分,我认为正确的行为是在数据库中存储数据时,如果不满足约束,就会失败,就像Jetty servlet一样。但我不想再奇怪为什么Tomcat没有失败
更新
此外,这个@NotEmpty注释是很久以前添加的。现在我又开始考虑这个问题了,它是一个NotEmpty,而不是NotNull约束,所以不管我的数据库是否允许null,我只是不想让这个字段为空(空字符串“”)。所以再一次,我猜为什么Tomcat让我存储一个电话号码为空的用户
?这已经有2年多的历史了,并且已经在10多个不同的项目中使用过(这是一个我在多个应用程序中使用的用户管理插件),而且在我切换到Jetty之前从未失败过
格雷德尔先生
一个常见的问题是类路径中存在重复的类(在不同版本上)。这可以很容易地解释你所看到的差异。在你的构建工具上运行一个重复的类检查插件(如果你还没有)。谢谢你的回复!我刚刚运行了一个“查找重复类”插件,但没有发现任何结果!还有其他想法吗?你能展示一下你的pom.xml吗?我将添加我的build.gradleOne。最常见的问题是,你的类路径中有重复的类(在不同的版本上)。这可以很容易地解释你所看到的差异。在你的构建工具上运行一个重复的类检查插件(如果你还没有)。谢谢你的回复!我刚刚运行了一个“查找重复类”插件,但没有发现任何结果!还有其他想法吗?你能展示一下你的pom.xml吗?我将添加我的build.gradle
buildscript {
ext {
springBootVersion = '1.5.21.RELEASE'
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
}
}
plugins {
id 'java'
}
apply plugin: 'org.springframework.boot'
apply from: 'buildscripts/jacoco.gradle'
group = 'ar.com.sebasira'
version = '0.1.2-SNAPSHOT'
sourceCompatibility = '1.8'
springBoot {
buildInfo()
}
repositories {
mavenCentral()
maven { url "https://repo.spring.io/snapshot" }
maven { url "https://repo.spring.io/milestone" }
maven { url 'https://jitpack.io' }
}
ext{
lombokVersion = '1.16.20'
}
configurations {
compile.exclude module: "spring-boot-starter-tomcat"
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-thymeleaf')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-mail')
compile('org.springframework.session:spring-session-jdbc')
// Use JETTY instead of Tomcat
compile("org.springframework.boot:spring-boot-starter-jetty")
compile('org.liquibase:liquibase-core:3.7.0')
compile("io.jsonwebtoken:jjwt:0.7.0")
compile('org.cryptonode.jncryptor:jncryptor:1.2.0')
compile("com.google.code.gson:gson:2.8.5")
compileOnly("org.projectlombok:lombok:${lombokVersion}")
annotationProcessor ("org.projectlombok:lombok:${lombokVersion}")
compile("com.github.darrachequesne:spring-data-jpa-datatables:4.3")
compile("org.springframework.boot:spring-boot-devtools")
compile("io.springfox:springfox-swagger2:2.9.2")
compile("io.springfox:springfox-swagger-ui:2.8.0")
runtime ('net.sourceforge.nekohtml:nekohtml:1.9.15')
runtime('mysql:mysql-connector-java:8.0.15')
testCompile('org.springframework.boot:spring-boot-starter-test')
testCompile('org.springframework.security:spring-security-test')
// InMemory DB
testCompile "com.h2database:h2:1.4.199"
// Mockito
compile "org.mockito:mockito-core:2.23.4"
}