问题描述
我有一个post端点,它将用于保存不同搜索表单的用户搜索设置。
每个搜索表单都有不同的搜索属性,需要特定的post请求。
每个post请求传输多个搜索属性值对。这些post请求将被发送到同一个端点,以将这些搜索设置保存到数据库中。
在保存到数据库之前,必须验证搜索设置(我的系统要求之一),并且每个搜索设置也有不同的验证过程。
可能的解决方法
我计划使用一个通用请求来处理其他特定请求。因此,我将创建一个抽象类 SearchSettingsRequest
以及其他 SpecificSearchSettingsRequest
子类(例如。 ProductSearchSettingsRequest
)从抽象类扩展而来。接下来,我将创建一个摘要 validate()
方法,该方法将在特定子类中实现。
public abstract class SearchSettingsRequest {
private String type;
// other field declarations, getters, setters...
public abstract boolean validate();
}
public class ProductSearchSettingsRequest extends SearchSettingsRequest{
@Override
public abstract boolean validate() {
return true;
}
}
对于端点,我发现我可以使用 JsonNode
.
@PostMapping("/columns")
public ResponseEntity<GeneralResponse> send(@RequestBody JsonNode jsonNode) {
ObjectMapper obj = new ObjectMapper();
if (jsonNode.has("type") {
ProductSearchSettingsRequest req = obj.convertValue(jsonNode, ProductSearchSettingsRequest.class);
// more code
}
else if () // more if-else for other request subclasses...
}
有没有更好的方法来实现这个端点?
暂无答案!
目前还没有任何答案,快来回答吧!