我正在使用動態查詢為我的演示專案 API 處理過濾器/搜索端點。這是我的過濾器端點。
@GetMapping("/filter")
public ResponseEntity<List<ProductDto>> filterProducts(@RequestParam("name") String name,
@RequestParam("expiryDatemin") String expiryDatemin, @RequestParam("expiryDatemax") String expiryDatemax,
@RequestParam("type") String type, @RequestParam("quantitymin") String quantityin,
@RequestParam("quantitymin") String quantitymax, @RequestParam("company") String company,
@RequestParam("pricemin") String pricemin, @RequestParam("pricemax") String pricemax) {
return new ResponseEntity<List<ProductDto>>(productgetservice.filterProducts(expiryDatemin, expiryDatemax, name,
type, quantityin, quantitymax, company, pricemin, pricemax), HttpStatus.OK);
}
我想做的是用戶可以輸入任何元素,并且只有滿足過濾器的資料才會出現。
如果用戶將所有值發送為 null 或空,它應該回傳表的完整資料。
就像最后具有 min max 的變數一樣,是在這些間隔之間獲取資料。
如果僅輸入最小值,則應發送高于該條件的所有資料,如果僅輸入最大值,則應顯示低于該條件的所有資料。
我的 serviceImpl 類方法如下:
@Override
public List<ProductDto> filterProducts(String expiryDatemin, String expiryDatemax, String name, String type,
String quantitymin, String quantitymax, String company, String pricemin, String pricemax) {
List<Product> products = productrepository.findAll(where(ProductSpecifications
.withExpiryDates(expiryDatemin, expiryDatemax).and(ProductSpecifications.withcompany(company))
.and(ProductSpecifications.withPrices(pricemin, pricemax))
.and(ProductSpecifications.withQuantity(quantitymin, quantitymax))
.and(ProductSpecifications.withType(type))));
return productsListToProductDto(products);
}
我遇到的問題是,當我使用“和”發送資料時,它總是回傳一個空陣列作為有效負載。當我用“或”替換和時,它將表的整個資料作為串列回傳。
這是我的產品規格類方法:
public static Specification<Product> withExpiryDates(String expiryDatemin, String expiryDatemax) {
return (root, query, cb) -> {
if ((expiryDatemax.isBlank() || expiryDatemax == null)
&& (expiryDatemin.isBlank() || expiryDatemin == null)) {
return cb.conjunction();
} else if ((expiryDatemax.isBlank() || expiryDatemax == null)) {
return cb.greaterThanOrEqualTo(root.get("expiryDate"), LocalDate.parse(expiryDatemin));
} else if ((expiryDatemin.isBlank() || expiryDatemin == null)) {
return cb.lessThanOrEqualTo(root.get("expiryDate"), LocalDate.parse(expiryDatemax));
} else {
return cb.between(root.get("expiryDate"), LocalDate.parse(expiryDatemin),
LocalDate.parse(expiryDatemax));
}
};}
public static Specification<Product> withPrices(String pricelow, String pricemax) {
return (root, query, cb) -> {
if ((pricelow.isBlank() || pricelow == null) && (pricemax.isBlank() || pricemax == null)) {
return cb.conjunction();
} else if ((pricemax.isBlank() || pricemax == null)) {
return cb.greaterThanOrEqualTo(root.get("price"), Double.valueOf(pricelow));
} else if ((pricelow.isBlank() || pricelow == null)) {
return cb.lessThanOrEqualTo(root.get("price"), Double.valueOf(pricemax));
} else {
return cb.between(root.get("price"), Double.valueOf(pricelow), Double.valueOf(pricemax));
}
};}
public static Specification<Product> withQuantity(String quantitymin, String quantitymax) {
return (root, query, cb) -> {
if ((quantitymin.isBlank() || quantitymin == null) && (quantitymax.isBlank() || quantitymax == null)) {
return cb.conjunction();
} else if ((quantitymax.isBlank() || quantitymax == null)) {
return cb.greaterThanOrEqualTo(root.get("quantity"), Integer.valueOf(quantitymin));
} else if ((quantitymin.isBlank() || quantitymin == null)) {
return cb.lessThanOrEqualTo(root.get("quantity"), Integer.valueOf(quantitymax));
} else {
return cb.between(root.get("quantity"), Integer.valueOf(quantitymin), Integer.valueOf(quantitymax));
}
};
}
public static Specification<Product> withType(String type) {
return (root, query, cb) -> {
if (type == null) {
return cb.conjunction();
} else
return cb.like(root.get("type"), type);
};
}
public static Specification<Product> withname(String name) {
return (root, query, cb) -> {
if (name == null) {
return cb.conjunction();
} else
return cb.like(root.get("name"), name);
};
}
public static Specification<Product> withcompany(String company) {
return (root, query, cb) -> {
if (company == null) {
return cb.conjunction();
} else
return cb.like(root.get("company"), company);
};
}
任何幫助,將不勝感激。我對 Spring/Spring 啟動相當陌生,而 SpecificationAPI 對我來說非常新。
uj5u.com熱心網友回復:
我做了以下更改,它對我來說很好。
在規范類中替換
return cb.conjunction();為。return null;.isBlank()從規范類中洗掉檢查。添加
.and(ProductSpecifications.withName(name))以過濾服務類中的產品方法。添加
required=false到您的所有@RequestParam @RequestParam(required=false)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/480015.html
