I have annotated my bean with some @NotNull
and use the spring @Valid
annotation in the @GetMapping
. But this did not work.
The only difference I see from other applications is that I use @EnableWebMvc
instead of @EnableWebFlux
.
In the controller:
@PostMapping(value = "/something")
public Mono<ResponseEntity> save(
@Valid @RequestBody MyBean mybean) {
return myService.save(myBean)
.map(RestResponses::ok)
.defaultIfEmpty(RestResponses.empty());
}
In the Application.java:
@SpringBootApplication
@EnableWebFlux
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
My bean class:
import org.springframework.data.annotation.Id;
import org.springframework.data.redis.core.RedisHash;
import org.springframework.data.redis.core.index.Indexed;
import javax.validation.constraints.NotNull;
import java.util.Objects;
@RedisHash("mybean")
public class MyBean {
@Id
private Long id;
@NotNull
@Indexed
private String name;
//getters, setters...
}
and pom.xml:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.0.M1</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
...
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
</dependency>
</dependencies>
Am I doing something wrong?
Actually there is some dependencies problem. In the dependencies you can see those two libraries:
org.hibernate:hibernate-validator:5.4.1.Final
javax.validation:validation-api:1.1.0.Final
And according to documentation Hibernate Validator you should provide additional dependency for Unified Expression Language
compile group: 'org.glassfish', name: 'javax.el', version: '3.0.1-b08'
After @Valid
annotation should work as expected.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.