Wenn derIn diesem Artikel wird erläutert, wie sichergestellt werden kann, dass alle Parameter in einer mit @RequestBody annotierten Entitätsklasse in Spring ungleich Null sind. Es erklärt das Standardverhalten von @RequestBody für Nullparameter und bietet mehrere Optionen zum Umgang mit Nullparametern Wenn eine mit @RequestBody annotierte Entitätsklasse ungleich Null ist, können Sie die Annotation
@NotNullaus dem Paketjavax.validationverwenden.import javax.validation.constraints.NotNull; public class MyEntity { @NotNull private String name; // Other fields }
@NotNull-Annotation auf ein Feld angewendet wird, prüft Spring Validation automatisch, ob das Feld nicht null ist. Wenn es null ist, wird eine
ConstraintViolationExceptionausgelöst.
Wie geht @RequestBody mit Nullparametern um?
@RequestBodyannotiert ist und der entsprechende Anforderungsparameter null ist, wird das Feld in der Entitätsklasse auf null gesetzt Entitätsklasse mit @RequestBody?Sie haben mehrere Möglichkeiten, mit der Situation umzugehen, wenn einige Parameter in einer Entitätsklasse mit @RequestBody null sind:@NotNullannotation from thejavax.validationpackage.
import javax.validation.constraints.DefaultValue; public class MyEntity { @RequestBody private String name; @DefaultValue("unknown") private String description; // Other fields }
When the@NotNullannotation is applied to a field, Spring Validation will automatically check if the field is non-null. If it is null, aConstraintViolationExceptionwill be thrown.
By default, @RequestBody will bind a null value to a non-primitive field in the entity class. For example, if you have a field annotated with@RequestBodyand the corresponding request parameter is null, the field will be set to null in the entity class.
You have several options to handle the situation when some parameters in an entity class with @RequestBody are null:
@DefaultValueannotation from thejavax.validationpackage.import java.util.Optional; public class MyEntity { @RequestBody private String name; private Optional description; // Other fields }
In this case, if thedescriptionparameter is null in the request, it will be set to "unknown" in the entity class.
Optionalwrapper class from thejava.utilpackage.In this case, if thedescriptionparameter is null in the request, thedescriptionfield in the entity class will be set toOptional.empty().
BadRequestException
@DefaultValueaus dem Paketjavax.validation.descriptionWenn der Parameter in der Anfrage null ist, wird er in der Entitätsklasse auf „unbekannt“ gesetzt.
Optionalaus dem Paketjava.util.rrreeeIn diesem Fall, wenn der Parameterdescriptionnull ist Bei der Anfrage wird das Felddescriptionin der Entitätsklasse aufOptional.empty()gesetzt.
BadRequestExceptionauslösen, wenn einer der erforderlichen Parameter null ist.Das obige ist der detaillierte Inhalt vonEinige Parameter in der Entitätsklasse, die mithilfe der @RequestBody-Annotation in Spring empfangen wurden, sind null. Für weitere Informationen folgen Sie bitte anderen verwandten Artikeln auf der PHP chinesischen Website!