我已经用一些注释我的 bean@NotNull
并@Valid
在@GetMapping
. 但这没有用。
我从其他应用程序中看到的唯一区别是我使用@EnableWebMvc
而不是@EnableWebFlux
.
在控制器中:
@PostMapping(value = "/something")
public Mono<ResponseEntity> save(
@Valid @RequestBody MyBean mybean) {
return myService.save(myBean)
.map(RestResponses::ok)
.defaultIfEmpty(RestResponses.empty());
}
在 Application.java 中:
@SpringBootApplication
@EnableWebFlux
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
我的豆类:
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...
}
和 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>
难道我做错了什么?