Java 为什么要将dto转换为Map?
我一直在试图理解为什么搜索被转换成地图。调用以下函数以获取与给定搜索词(来自角度形式)匹配的客户Java 为什么要将dto转换为Map?,java,Java,我一直在试图理解为什么搜索被转换成地图。调用以下函数以获取与给定搜索词(来自角度形式)匹配的客户 public Page<Customer> getCustomerFromCustomerLoan(Object searchDto, Pageable pageable) { List<Customer> customerList = new ArrayList<>(); final ObjectMapper objectMap
public Page<Customer> getCustomerFromCustomerLoan(Object searchDto, Pageable pageable) {
List<Customer> customerList = new ArrayList<>();
final ObjectMapper objectMapper = new ObjectMapper();
Map<String, String> s = objectMapper.convertValue(searchDto, Map.class);
s.put("distinctByCustomer", "true");
s.values().removeIf(Objects::isNull);
logger.info("search param for customer in customerLoan {}", s);
final CustomerLoanSpecBuilder customerLoanSpecBuilder = new CustomerLoanSpecBuilder(s);
final Specification<CustomerLoan> specification = customerLoanSpecBuilder.build();
Page<CustomerLoan> customerLoanPage = customerLoanRepository
.findAll(specification, pageable);
customerLoanPage.getContent().forEach(customerLoan -> {
if (!customerList.contains(customerLoan)) {
customerList.add(customerLoan.getCustomerInfo());
}
});
List<Customer> finalList = customerList.stream().filter(distinctByKey(Customer::getId))
.collect(
Collectors.toList());
Page<Customer> pages = new PageImpl<Customer>(finalList, pageable,
customerLoanPage.getTotalElements());
return pages;
}
公共页面getCustomerFromCustomerLoan(对象搜索到,可分页){
List customerList=new ArrayList();
最终ObjectMapper ObjectMapper=新ObjectMapper();
Map s=objectMapper.convertValue(searchDto,Map.class);
s、 put(“distinctByCustomer”、“true”);
s、 values().removeIf(Objects::isNull);
info(“在customerLoan{}中搜索客户参数”;
最终CustomerLoanSpecBuilder CustomerLoanSpecBuilder=新CustomerLoanSpecBuilder;
最终规范=customerLoanSpecBuilder.build();
页面customerLoanPage=customerLoanRepository
.findAll(规范,可分页);
customerLoanPage.getContent().forEach(customerLoan->{
如果(!customerList.contains(customerLoan)){
add(customerLoan.getCustomerInfo());
}
});
List finalList=customerList.stream().filter(distinctByKey(Customer::getId))
.收集(
收藏家;
Page pages=新的PageImpl(最终列表,可分页,
customerLoanPage.getTotalElements());
返回页面;
}
好吧,当问为什么有人以特定的方式做某事时,我们所能做的就是做出有根据的猜测——除非有关于这背后的推理的文档。在这种情况下,我的猜测是,由于searchDto
属于Object
类型,因此它实际上可能是几种类型中的一种,例如,普通的json字符串、已解析的pojo等。将其转换为映射会使该dto中的信息以相当通用的方式可用
考虑一下实际使用不同POJO的API,这些POJO共享一些属性,例如CustomerLoan
,比如customerNumber
,loanAmount
,customerName
等等
一个DTO现在可以使用只有customerNumber
和loanAmount
的DTO,而另一个DTO有customerNumber
和customerName
。如果将这些DTO转换为映射,则会得到一个通用结构,可以将该结构传递给CustomerLoanSpecBuilder
,然后该类可以检查这些属性是否有值
诚然,您可能会将接口与instanceof
一起使用,但这也可能会很乏味
我将尝试用一个例子来说明:
使用地图可能如下所示:
Map<String, String> props = ...;
if( props.get("customerNumber") != null ) {
//add predicate for customer number
}
if( props.get("customerName") != null ) {
//add predicate for customer name
}
if( props.get("loanAmount") != null ) {
//add predicate for loan amount
}
Object inputDto = ...;
if( inputDto instanceof ICustomerNumber ) {
//cast to ICustomerNumber, get the value and add predicate for customer number
}
if( inputDto instanceof ICustomerName ) {
//cast to ICustomerName , get the value and add predicate for customer name
}
if( inputDto instanceof ILoanAmount ) {
//cast to ILoanAmount , get the value and add predicate for loan amount
}
现在使用这些可以如下所示:
Map<String, String> props = ...;
if( props.get("customerNumber") != null ) {
//add predicate for customer number
}
if( props.get("customerName") != null ) {
//add predicate for customer name
}
if( props.get("loanAmount") != null ) {
//add predicate for loan amount
}
Object inputDto = ...;
if( inputDto instanceof ICustomerNumber ) {
//cast to ICustomerNumber, get the value and add predicate for customer number
}
if( inputDto instanceof ICustomerName ) {
//cast to ICustomerName , get the value and add predicate for customer name
}
if( inputDto instanceof ILoanAmount ) {
//cast to ILoanAmount , get the value and add predicate for loan amount
}
另一种方法是使用反射来检查是否有一个名为
getCustomerNumber()
的方法,如果有,则调用它,但您不想走这条路,当我们问某人为什么以某种特定的方式做某事时,我们所能做的就是做出有根据的猜测——除非有关于这背后的原因的文档。在这种情况下,我的猜测是,由于searchDto
属于Object
类型,因此它实际上可能是几种类型中的一种,例如,普通的json字符串、已解析的pojo等。将其转换为映射会使该dto中的信息以相当通用的方式可用
考虑一下实际使用不同POJO的API,这些POJO共享一些属性,例如CustomerLoan
,比如customerNumber
,loanAmount
,customerName
等等
一个DTO现在可以使用只有customerNumber
和loanAmount
的DTO,而另一个DTO有customerNumber
和customerName
。如果将这些DTO转换为映射,则会得到一个通用结构,可以将该结构传递给CustomerLoanSpecBuilder
,然后该类可以检查这些属性是否有值
诚然,您可能会将接口与instanceof
一起使用,但这也可能会很乏味
我将尝试用一个例子来说明:
使用地图可能如下所示:
Map<String, String> props = ...;
if( props.get("customerNumber") != null ) {
//add predicate for customer number
}
if( props.get("customerName") != null ) {
//add predicate for customer name
}
if( props.get("loanAmount") != null ) {
//add predicate for loan amount
}
Object inputDto = ...;
if( inputDto instanceof ICustomerNumber ) {
//cast to ICustomerNumber, get the value and add predicate for customer number
}
if( inputDto instanceof ICustomerName ) {
//cast to ICustomerName , get the value and add predicate for customer name
}
if( inputDto instanceof ILoanAmount ) {
//cast to ILoanAmount , get the value and add predicate for loan amount
}
现在使用这些可以如下所示:
Map<String, String> props = ...;
if( props.get("customerNumber") != null ) {
//add predicate for customer number
}
if( props.get("customerName") != null ) {
//add predicate for customer name
}
if( props.get("loanAmount") != null ) {
//add predicate for loan amount
}
Object inputDto = ...;
if( inputDto instanceof ICustomerNumber ) {
//cast to ICustomerNumber, get the value and add predicate for customer number
}
if( inputDto instanceof ICustomerName ) {
//cast to ICustomerName , get the value and add predicate for customer name
}
if( inputDto instanceof ILoanAmount ) {
//cast to ILoanAmount , get the value and add predicate for loan amount
}
另一种选择是使用反射来检查是否有一个名为getCustomerNumber()
的方法,如果是这样,则调用它,但您不想走这条路