Ich habe meine Bohne mit einigen @NotNull
kommentiert und die Quelle @Valid
Annotation in der @GetMapping
verwenden. Aber das hat nicht funktioniert.Unterstützt spring webflux die Validierung von Java-Bean?
Der einzige Unterschied, den ich von anderen Anwendungen sehe, ist, dass ich @EnableWebMvc
anstelle von @EnableWebFlux
verwende.
In der Steuerung:
@PostMapping(value = "/something")
public Mono<ResponseEntity> save(
@Valid @RequestBody MyBean mybean) {
return myService.save(myBean)
.map(RestResponses::ok)
.defaultIfEmpty(RestResponses.empty());
}
Im Application.java:
@SpringBootApplication
@EnableWebFlux
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Meine Bean-Klasse:
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...
}
und 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>
Mache ich etwas falsch?
Könnten Sie bitte Code Ihrer Get-Methode hinzufügen? –
@Orest, fertig! Danke für Ihr Interesse. –
Und 'MyBean' Klasse. Auch welche Springboot Version hast du '2.0.0M1'? –