[英]Does spring webflux supports javax bean validation?
I have annotated my bean with some @NotNull
and use the spring @Valid
annotation in the @GetMapping
. 我用一些
@NotNull
注释了我的bean,并在@GetMapping
使用spring @Valid
注释。 But this did not work. 但这没有用。
The only difference I see from other applications is that I use @EnableWebMvc
instead of @EnableWebFlux
. 我与其他应用程序看到的唯一区别是,我使用
@EnableWebMvc
而不是@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: 在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: 和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 并且根据文档Hibernate Validator,您应该提供统一表达语言的其他依赖关系
compile group: 'org.glassfish', name: 'javax.el', version: '3.0.1-b08'
After @Valid
annotation should work as expected. @Valid
之后,注释应按预期工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.