Frage: Es ist möglich, die JSON -Nutzlast eines Anforderungsbehörde zu validieren, ohne speziell zu schreiben, ob Anweisungen angerufen werden? Vielleicht über Annotation oder Konfiguration?public class Foo {
private int important;
private String something;
//constructors, getter, seters, toString
}
< /code>
und eine sehr einfache Controller -Klasse: < /p>
@SpringBootApplication
@RestController
public class QuestionController {
public static void main(String[] args) {
SpringApplication.run(QuestionController.class, args);
}
@GetMapping(value = "/question")
Mono question(@RequestBody Foo foo) {
System.out.println("The object foo, with value for important = " + foo.getImportant() + " and something = " + foo.getSomething());
return Mono.just("question");
}
}
< /code>
Wenn ich mit einer Nutzlast abfragt, z. B.: < /p>
{
"important": 42,
"something": "value"
}
< /code>
Alles funktioniert vollkommen in Ordnung, sehr glücklich.{
"importantWithTypo": 42,
"something": "value"
}
< /code>
Oder der erforderliche "wichtige" fehlt (beachten Sie, dass JSON nicht einmal vollständig ist) < /p>
{
"something": "value"
}
< /code>
Die Anforderung und die Berechnung sind noch gültig! Und der Wert von "wichtig" ist 0! @GetMapping(value = "/question")
Mono question(@RequestBody Foo foo) {
if (0 == foo.getImportant()) {
throw new IllegalArgumentException();
}
System.out.println("The object foo, with value for important = " + foo.getImportant() + " and something = " + foo.getSomething());
return Mono.just("question");
}
< /code>
Was ist der effizienteste Weg, dies zu beheben? Eine Art Anmerkung? Oder vielleicht Spring Boot -Konfiguration?
Danke
Federschuh - Validieren Sie @RequestBody ⇐ Java
-
- Similar Topics
- Replies
- Views
- Last post
-
-
Wildcard (**) Pfad, die nicht alle anderen Wege im Federschuh/Winkel -App fangen
by Anonymous » » in Java - 0 Replies
- 4 Views
-
Last post by Anonymous
-