【发布时间】:2021-10-23 08:31:53
【问题描述】:
我试图在我的 api 中接收一个包含城市名称的数组,并创建一个带有返回该城市列表的标准 bulder 的过滤器。
这就是我的控制器中的内容
@GetMapping("/weather")
public @ResponseBody List<Weather> weatherGetALL(
@RequestParam(required = false) String order,
@RequestParam(required = false) List<String> city,
@DateTimeFormat(pattern = DATE_PATTERN) Date date) {
return this.weatherService.findAll(order, city, date);
}
这就是我在服务中应用这些类型的过滤器的原因
@Autowired
WeatherRepository _weatherRepo;
public Weather save(Weather weather) {
return this._weatherRepo.save(weather);
}
public Weather findeById(Integer id) {
return this._weatherRepo.findById(id).orElse(null);
}
public List<Weather> findAll(String order, List<String> city, Date date ) {
List<Weather> weather = this._weatherRepo.findAll((Specification<Weather>)(root, cq, cb) -> {
Predicate p = cb.conjunction();
if(!(city == null)){
System.out.println("entra en el arreglo");
// for (String name : city) {
// p = cb.and(p, cb.equal(root.get("city"), name));
// }
}
/*if (!StringUtils.isEmpty(city)) {
System.out.println("ciudad:" + name);
p = cb.and(p, cb.equal(root.get("city"), city ));
}*/
if (Objects.nonNull(date)) {
p = cb.and(p, cb.equal(root.get("date"), date ));
}
if (!StringUtils.isEmpty(order)) {
if (!StringUtils.isEmpty(order)) {
switch(order) {
case "date":
cq.orderBy(cb.asc(root.get("date")));
break;
case "-date":
cq.orderBy(cb.desc(root.get("date")));
break;
default:
cq.orderBy(cb.asc(root.get("id")));
}
}
}
return p;
});
return weather;
}
我用“in”操作符读到了,我可以做到,但我还没有找到正确的方法来应用它。显然我收到的一切都很好,唯一让我失望的是那部分的过滤
【问题讨论】:
标签: java spring-boot spring-data-jpa criteria-api criteriaquery