Wie kann ich eine neue Validierungsregel hinzufügen, ohne dass sich dies auf andere und übergeordnete Felder auswirkt?Java

Java-Forum
Guest
 Wie kann ich eine neue Validierungsregel hinzufügen, ohne dass sich dies auf andere und übergeordnete Felder auswirkt?

Post by Guest »

Ich habe eine Klasse PojoB, die einen Feldcode und ein Feld der Klasse PojoA enthält:
@Data
public class PojoB {

@Size(min = 1, max = 5)
@NotBlank()
@Pattern(regexp = "[0-9]+")
private String code;

@Valid
private PojoA pojoA;

}

@Data
public class PojoA {

@Size(min = 1, max = 5)
@NotBlank()
@Pattern(regexp = "[0-9]+")
private String code;
}


Und PojoB wird als Anforderungstext von /testSpringValidation1 verwendet, Validierungen sind aktiviert.
Nun zum Neuen API /testSpringValidation2, der pojoA.code kann mehr Zeichen zulassen und keine Änderung/Unterschied bei anderen Validierungsregeln im Vergleich zu /testSpringValidation1. Daher wird @Pattern(regexp = "[0-9a-z]+", groups = LessValidation.class) hinzugefügt. Aber wie kann ich es implementieren, ohne vorhandenen Code zu ändern?
public interface LessValidation extends Default {
}

@PostMapping("/testSpringValidation2")
public PojoB testSpringValidation2(@RequestBody @Validated({LessValidation.class}) PojoB reqBody) {
return reqBody;
}

@Data
public class PojoA {

@Size(min = 1, max = 5)
@NotBlank()
@Pattern(regexp = "[0-9]+")
@Pattern(regexp = "[0-9a-z]+", groups = LessValidation.class)
private String code;
}

Beim Auslösen der API /testSpringValidation2 durch diesen Reqbody,
{
"code":"12345",
"pojoA":{
"code": "12ab"
}
}

Ich habe diesen Fehler erhalten
error:
{
"type": "error",
"code": "400",
"details": "mandatory parameter pojoA.code is invalid: must match \"[0-9]+\""
}

Aber wenn ich LessValidation nicht als Standard erweitere, werden auch andere Validierungsregeln (wie Validierungen auf pojoB.code) übersprungen.

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post
  • Wie mache ich rein inline?
    by Guest » » in HTML
    0 Replies
    11 Views
    Last post by Guest
  • Wie wechsle ich zum neuen Fenster, das sofort eine Warnung auswirkt?
    by Anonymous » » in Java
    0 Replies
    8 Views
    Last post by Anonymous
  • CI4: Die Übergabe der Variablen an benutzerdefinierte Validierungsregel kommt als Zeichenfolge nicht int int
    by Guest » » in Php
    0 Replies
    11 Views
    Last post by Guest
  • JQuery - Warum unterstützt die Validierungsregel den vordefinierten Wert nicht?
    by Anonymous » » in Jquery
    0 Replies
    0 Views
    Last post by Anonymous
  • PIP sagt, dass Python-Magic installiert ist, aber Substanzed sagt, dass dies nicht der Fall ist
    by Anonymous » » in Python
    0 Replies
    16 Views
    Last post by Anonymous
  • cron