[ad_1]
I recently upgraded to Spring Boot 2.7.0, and my the request body validations stopped working. From my research so far, I’ve learnt that the
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
is required after Spring Boot 2.3.0.
I’ve added the dependency and verified that it is the only validation jar on the classpath is jakarta.validation
(not javax.validation
).
Here’s the Controller class:
@RestController
@RequestMapping("v1/services")
@Validated
public class ServiceController {
@PutMapping(
path = "/{id}",
consumes = APPLICATION_JSON_VALUE,
produces = APPLICATION_JSON_VALUE
)
@Validated(OnUpdate.class)
public ResponseEntity<Service> updateService(@PathVariable("id") int serviceId,
@Valid @RequestBody Service service) {
// Implementation omitted for brevity
}
}
Here is the domain class:
@Component
public class Service {
private int id;
NotNull(message = "Current status is required", groups = {OnCreate.class, OnUpdate.class})
private ServiceStatus currentStatus; // The field I want to validate
}
My partial pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
<relativePath/> <!-- lookup parent from repository --
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.2</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>7.0.4.Final</version>
</dependency>
My dependency tree:
[INFO] +- org.springframework.boot:spring-boot-starter:jar:2.7.0:compile
[INFO] | +- org.springframework.boot:spring-boot:jar:2.7.0:compile
[INFO] | | \- org.springframework:spring-context:jar:5.3.20:compile
[INFO] | +- org.springframework.boot:spring-boot-autoconfigure:jar:2.7.0:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-logging:jar:2.7.0:compile
[INFO] | | +- ch.qos.logback:logback-classic:jar:1.2.11:compile
[INFO] | | | \- ch.qos.logback:logback-core:jar:1.2.11:compile
[INFO] | | +- org.apache.logging.log4j:log4j-to-slf4j:jar:2.17.2:compile
[INFO] | | | \- org.apache.logging.log4j:log4j-api:jar:2.17.2:compile
[INFO] | | \- org.slf4j:jul-to-slf4j:jar:1.7.36:compile
[INFO] | +- jakarta.annotation:jakarta.annotation-api:jar:1.3.5:compile
[INFO] | +- org.springframework:spring-core:jar:5.3.20:compile
[INFO] | | \- org.springframework:spring-jcl:jar:5.3.20:compile
[INFO] | \- org.yaml:snakeyaml:jar:1.30:compile
[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.7.0:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-json:jar:2.7.0:compile
[INFO] | | +- com.fasterxml.jackson.core:jackson-databind:jar:2.13.3:compile
[INFO] | | | \- com.fasterxml.jackson.core:jackson-annotations:jar:2.13.3:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jdk8:jar:2.13.3:compile
[INFO] | | +- com.fasterxml.jackson.datatype:jackson-datatype-jsr310:jar:2.13.3:compile
[INFO] | | \- com.fasterxml.jackson.module:jackson-module-parameter-names:jar:2.13.3:compile
[INFO] | +- org.springframework.boot:spring-boot-starter-tomcat:jar:2.7.0:compile
[INFO] | | +- org.apache.tomcat.embed:tomcat-embed-core:jar:9.0.63:compile
[INFO] | | \- org.apache.tomcat.embed:tomcat-embed-websocket:jar:9.0.63:compile
[INFO] | +- org.springframework:spring-web:jar:5.3.20:compile
[INFO] | | \- org.springframework:spring-beans:jar:5.3.20:compile
[INFO] | \- org.springframework:spring-webmvc:jar:5.3.20:compile
[INFO] | +- org.springframework:spring-aop:jar:5.3.20:compile
[INFO] | \- org.springframework:spring-expression:jar:5.3.20:compile
[INFO] +- org.springframework.boot:spring-boot-starter-jdbc:jar:2.7.0:compile
[INFO] | +- com.zaxxer:HikariCP:jar:4.0.3:compile
[INFO] | | \- org.slf4j:slf4j-api:jar:1.7.36:compile
[INFO] | \- org.springframework:spring-jdbc:jar:5.3.20:compile
[INFO] | \- org.springframework:spring-tx:jar:5.3.20:compile
[INFO] +- org.springframework.boot:spring-boot-starter-validation:jar:2.7.0:compile
[INFO] | \- org.apache.tomcat.embed:tomcat-embed-el:jar:9.0.63:compile
[INFO] +- org.springframework.boot:spring-boot-devtools:jar:2.7.0:compile
[INFO] +- org.springdoc:springdoc-openapi-ui:jar:1.6.9:compile
[INFO] | +- org.springdoc:springdoc-openapi-webmvc-core:jar:1.6.9:compile
[INFO] | | \- org.springdoc:springdoc-openapi-common:jar:1.6.9:compile
[INFO] | | \- io.swagger.core.v3:swagger-core:jar:2.2.0:compile
[INFO] | | +- org.apache.commons:commons-lang3:jar:3.12.0:compile
[INFO] | | +- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.13.3:compile
[INFO] | | +- io.swagger.core.v3:swagger-annotations:jar:2.2.0:compile
[INFO] | | \- io.swagger.core.v3:swagger-models:jar:2.2.0:compile
[INFO] | +- org.webjars:swagger-ui:jar:4.11.1:compile
[INFO] | \- org.webjars:webjars-locator-core:jar:0.50:compile
[INFO] | \- com.fasterxml.jackson.core:jackson-core:jar:2.13.3:compile
[INFO] +- io.github.classgraph:classgraph:jar:4.8.147:compile
[INFO] +- jakarta.validation:jakarta.validation-api:jar:3.0.2:compile
[INFO] +- org.hibernate.validator:hibernate-validator:jar:7.0.4.Final:compile
[INFO] | +- org.jboss.logging:jboss-logging:jar:3.4.3.Final:compile
[INFO] | \- com.fasterxml:classmate:jar:1.5.1:compile
[INFO] +- com.h2database:h2:jar:2.1.212:runtime
[INFO] +- org.springframework.boot:spring-boot-starter-test:jar:2.7.0:test
[INFO] | +- org.springframework.boot:spring-boot-test:jar:2.7.0:test
[INFO] | +- org.springframework.boot:spring-boot-test-autoconfigure:jar:2.7.0:test
[INFO] | +- com.jayway.jsonpath:json-path:jar:2.7.0:test
[INFO] | | \- net.minidev:json-smart:jar:2.4.8:test
[INFO] | | \- net.minidev:accessors-smart:jar:2.4.8:test
[INFO] | | \- org.ow2.asm:asm:jar:9.1:test
[INFO] | +- jakarta.xml.bind:jakarta.xml.bind-api:jar:2.3.3:compile
[INFO] | | \- jakarta.activation:jakarta.activation-api:jar:1.2.2:compile
[INFO] | +- org.assertj:assertj-core:jar:3.22.0:test
[INFO] | +- org.hamcrest:hamcrest:jar:2.2:test
[INFO] | +- org.junit.jupiter:junit-jupiter:jar:5.8.2:test
[INFO] | | +- org.junit.jupiter:junit-jupiter-api:jar:5.8.2:test
[INFO] | | +- org.junit.jupiter:junit-jupiter-params:jar:5.8.2:test
[INFO] | | \- org.junit.jupiter:junit-jupiter-engine:jar:5.8.2:test
[INFO] | +- org.mockito:mockito-core:jar:4.5.1:test
[INFO] | | +- net.bytebuddy:byte-buddy:jar:1.12.10:test
[INFO] | | +- net.bytebuddy:byte-buddy-agent:jar:1.12.10:test
[INFO] | | \- org.objenesis:objenesis:jar:3.2:test
[INFO] | +- org.mockito:mockito-junit-jupiter:jar:4.5.1:test
[INFO] | +- org.skyscreamer:jsonassert:jar:1.5.0:test
[INFO] | | \- com.vaadin.external.google:android-json:jar:0.0.20131108.vaadin1:test
[INFO] | +- org.springframework:spring-test:jar:5.3.20:test
[INFO] | \- org.xmlunit:xmlunit-core:jar:2.9.0:test
[INFO] +- org.spockframework:spock-core:jar:2.1-groovy-3.0:test
[INFO] | +- org.codehaus.groovy:groovy:jar:3.0.10:test
[INFO] | \- org.junit.platform:junit-platform-engine:jar:1.8.2:test
[INFO] | +- org.opentest4j:opentest4j:jar:1.2.0:test
[INFO] | +- org.junit.platform:junit-platform-commons:jar:1.8.2:test
[INFO] | \- org.apiguardian:apiguardian-api:jar:1.1.2:test
[INFO] +- org.spockframework:spock-spring:jar:2.1-groovy-3.0:test
[INFO] +- org.codehaus.groovy:groovy-json:jar:3.0.10:test
[INFO] \- junit:junit:jar:4.13.2:test
[INFO] \- org.hamcrest:hamcrest-core:jar:2.2:test
Lastly, I’m confident it’s not the jakarta package because the following unit test passed:
import jakarta.validation.ConstraintViolation
import jakarta.validation.Validation
import jakarta.validation.Validator
def 'Null status on update should trigger violation'() {
given: 'I have an invalid service'
final LocalDate may4th2021 = LocalDate.of(2021, 5, 4)
Service serviceWithoutStatus = new Service(
id: 1,
contractId: 2,
dueDate: may4th2021,
currentStatus: null, // N.B. the violation
history: []
)
when: 'I validate the client'
Validator validator = Validation.byDefaultProvider()
.configure()
.messageInterpolator(new ParameterMessageInterpolator())
.buildValidatorFactory()
.getValidator()
Set<ConstraintViolation<Service>> violations = validator.validate(serviceWithoutStatus, OnUpdate.class)
then: 'There should be violations'
!violations.isEmpty()
violations.size() == 1
}
Edit: I am using Java 11
openjdk version "11.0.9.1" 2020-11-04 LTS
OpenJDK Runtime Environment Zulu11.43+1015-CA (build 11.0.9.1+1-LTS)
OpenJDK 64-Bit Server VM Zulu11.43+1015-CA (build 11.0.9.1+1-LTS, mixed mode)
Any help would greatly be appreciated. Thank you.
[ad_2]