[英]Springboot API endpoint, "dependent-optional" parameters
端點的查詢 object 有 5 個屬性,其中 2 個是必需的。 但是有 2 個特定的用例,要么只提供 2 個,要么全部提供。 即:無法提供field1-field2-field5。 可能會引發異常或驗證方法可能失敗。
這種可選的依賴關系如何反映在代碼中?
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 版本:2.2.13.RELEASE
Java版本:1.8.0_232
查詢 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
}
controller class,ControllerClass.java:
public class ControllerClass{
public FooResponse fooEndpoint(@Valid @RequestBody FooQuery params) throws Exception {
return foo.doBar(params);
}
}
我不確定我是否正確地遵循了您的問題! 但是對於組驗證,您可以在 Controller Class 頂部使用@Validated
和注釋
查看這篇文章,看看這是否有效!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.