过滤nonn none null值来自Java中的对象
我有一个带有某些端点的应用程序的弹簧靴,其中一个端点是搜索汽车选项,请参阅下面的
@PostMapping("/searchCarOptions")
public ResponseEntity<Cars> search(
@Parameter(description = "some searchRequest") @RequestBody SearchRequest searchRequest) {
if (searchRequest.getType() != null) {
//dosomthing
}
if (searchRequest.getBrand() != null) {
//dosomthing
}
if (searchRequest.getCountry() != null) {
//dosomthing
}
if (searchRequest.getClient() != null) {
//dosomthing
}
if (searchRequest.getAirco() != null) {
//dosomthing
}
etc.....
return ResponseEntity.ok(someService.search(searchRequest));
}
方法,可以将我的searchRequest放在方法中,然后返回填充的搜索条件,例如将其放入字符串中多变的?它将使代码清洁器。...
下面是我的模型
public class SearchRequest {
private String type;
private String brand;
private LocalDate date;
private String country;
private String client;
private String airco;
private String transmission;
private String seats;
getters and setters
}
I have a spring boot a application with some endpoints, one of the endpoints is to search for car options, see below
@PostMapping("/searchCarOptions")
public ResponseEntity<Cars> search(
@Parameter(description = "some searchRequest") @RequestBody SearchRequest searchRequest) {
if (searchRequest.getType() != null) {
//dosomthing
}
if (searchRequest.getBrand() != null) {
//dosomthing
}
if (searchRequest.getCountry() != null) {
//dosomthing
}
if (searchRequest.getClient() != null) {
//dosomthing
}
if (searchRequest.getAirco() != null) {
//dosomthing
}
etc.....
return ResponseEntity.ok(someService.search(searchRequest));
}
Is there better way to put my searchRequest in a method and return just the filled search criteria and put it for example in a String variable? it will make code cleaner....
Below is my model
public class SearchRequest {
private String type;
private String brand;
private LocalDate date;
private String country;
private String client;
private String airco;
private String transmission;
private String seats;
getters and setters
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从上面了解的是,您正在尝试丰富
searchRequest
,然后再将其传递给someService#search
。我认为您可以实施一些自定义的策略模式。让我们看看如何:创建一个界面searchRequestenricher,如下所示,
为每个字段创建searchRequestenricher实现(例如,我将仅在这里创建2个)
typeenricher
brandenricher,
现在您可以在控制器中以这种方式在控制器中使用这些富集
,您的控制器是干净的,并且不干净具有任何业务逻辑(因为它应该没有)。您正在遵循关注和单一责任原则的分离。如果在
searchRequest
中添加或删除额外的参数,则无需触摸控制器,并且想验证/富集它们。希望这会有所帮助。
What I understand from above is that you are trying to enrich your
SearchRequest
before passing it toSomeService#search
. I think you can implement little bit customised strategy pattern. Let's see how:Create an interface SearchRequestEnricher like below
Create SearchRequestEnricher implementations for each field(I will create only 2 just for example here)
TypeEnricher
BrandEnricher
Now you can use these enrichers in your controller
This way, your controller is clean and it doesn't have any business logic(as it should not have). You are following separation of concern and single responsibility principles. You don't need to touch your controller if you add or remove extra parameter in
SearchRequest
and would like to validate/enrich them.Hope this helps.