The query object of the endpoint has 5 properties and 2 of them are mandatory. But there are 2 certain use cases, either only 2 of them will be provided, or all of them will be provided. ie: field1-field2-field5 can't be provided. AN exception can be thrown or validation method can fail.
How can this optional dependency can be reflected in the code?
Extra question, during the api call, does the controller fill the values of the query object using the controller or setter methods (or how)?
Springboot version: 2.2.13.RELEASE
Java version: 1.8.0_232
The query class, FooQuery.java
@NoArgsConstructor
@AllArgsConstructor
@RequiredArgsConstructor
@Getter
@Setter
public class FooQuery {
@NonNull
@NotNull(message = "Field can not be null or empty")
@NotEmpty(message = "Field can not be null or empty")
@NotBlank(message = "Field can not be null or empty")
private String field1; //Mandatory
@NonNull
@NotNull(message = "Field can not be null or empty")
@NotEmpty(message = "Field can not be null or empty")
@NotBlank(message = "Field can not be null or empty")
private String field2; //Mandatory
private String[] fields3; //Optional
private String field4; //Optional
private String field5; //Optional
}
The controller class, ControllerClass.java:
public class ControllerClass{
public FooResponse fooEndpoint(@Valid @RequestBody FooQuery params) throws Exception {
return foo.doBar(params);
}
}
I am not sure if I followed your question correctly! But for group validation You can make use of @Validated
and annotate on top of your Controller Class
Check out this article to see if this works !
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.