JavaSpringBootHibernate验证在Tomcat和Jetty中的行为不同
我开发了许多Spring Boot应用程序(使用旧版本1.5.21.RELEASE),我一直使用Tomcat,因为它是Spring Boot中的默认版本
现在我需要切换到Jetty,这不是什么主要任务,只是排除了Tomcat并添加了Jetty依赖项。到目前为止还不错
当我第一次使用干净的数据库运行它时,它由于ConstraintViolation
而失败,查看它是我在User
实体上设置的自定义验证消息。这是我的实体:
@Entity
@Table(name="users")
@Data
public class User{
@Id
@Column(name = "username")
@NotEmpty(message = "*Please provide your username")
@JsonView(DataTablesOutput.View.class)
private String username;
@Column(name = "password")
@Length(min = 5, message = "*Your password must have at least 5 characters")
@NotEmpty(message = "*Please provide your password")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)
private String password;
@Column(name = "email")
@Email(message = "*Please provide a valid Email")
@NotEmpty(message = "*Please provide an email")
@JsonView(DataTablesOutput.View.class)
private String email;
@Column(name = "telephone")
@NotEmpty(message = "*Please provide a telephone number")
private String telephone;
}
在users表中telephone
列允许空值。实体中使用的验证是针对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让我存储一个电话号码为空的User
?这已经有2年多的历史了,并且已经在10多个不同的项目中使用过(这是一个我在多个应用程序中使用的用户管理插件),而且在我切换到Jetty之前从未失败过
建造。梯度
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"
}
共 (0) 个答案