无法使用带有QueryDSL和Web支持的Spring Data Repository在集合中查询字段

时间:2018-08-15 00:17:38

标签: spring mongodb spring-boot spring-data querydsl

我已经使用Spring Boot版本1.5.12,Spring Data MongoDB和带有Web支持的QueryDSL实现了Rest API。每当我尝试执行如下(http://localhost:8080/loads?itemName=Towel&itemIds=5b20677e8bb7390001a16ab8,5b20677e8bb7390001a16abs)这样的查询时,都会出现以下错误:

nest()

我希望查询包含“ like”,“ startsWith”,“ endsWith”和“ equals ignore case”在内的模型类。我以某种方式实施的机制将其强制应用于模型(项目)中的Set。我一直在尝试解决该问题,但没有成功。如果这里的社区可以帮助我解决此问题,我将不胜感激。下面是我的代码。

模型类:

library(tidyverse)

test %>%
  nest(-row_numb) %>%
  mutate(Dictionary = map(data, unlist),
         Dictionary = map_chr(Dictionary, paste, collapse = ", "))

#> # A tibble: 3 x 3
#>   row_numb data           Dictionary                                      
#>      <dbl> <list>         <chr>                                           
#> 1        1 <tibble [7 × … apply, assistance, benefit, compass, medical, o…
#> 2        2 <tibble [3 × … meet, service, website                          
#> 3        3 <tibble [8 × … center, country, country, develop, highly, home…

控制器层:

{
    "status": 500,
    "message": "com.querydsl.core.types.dsl.SetPath cannot be cast to com.querydsl.core.types.dsl.StringPath",
    "detailedMessage": "ClassCastException: com.querydsl.core.types.dsl.SetPath cannot be cast to com.querydsl.core.types.dsl.StringPath"
}

服务层:

@Document
@QueryEntity
@ApiModel
public class Item {
  @Id
  private String id;
  private int itemSize;
  private String itemName;
  private Set<String> itemIds = new HashSet();

  ...

存储层:

  @GetMapping
  @ApiOperation(value = "Get Items", notes = "Returns Items")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "Authorization", value = "Authorization Bearer JWT Token",
          required = true, dataType = "string", paramType = "header")
  })
  public ResponseEntity<ItemResponse> list(
      @QuerydslPredicate(root = Item.class) final Predicate predicate,
      final Pageable pageable) {

    ItemResponse data = ItemService.list(predicate, pageable);

    if (data == null) {
      return new ResponseEntity<>(HttpStatus.NOT_FOUND);
    }
    return new ResponseEntity<>(data, HttpStatus.OK);
  }

驱动程序类:

  @Override
  public ItemResponse list(final Predicate predicate, final Pageable pageable) {

    ItemResponse ItemResponse = new ItemResponse();

    ItemResponse.setCount(ItemRepository.count(predicate));
    ItemResponse.setData(Lists.newArrayList(ItemRepository.findAll(predicate, pageable)));

    return ItemResponse;
  }

Pom.xml

@Repository
public interface ItemRepository extends MongoRepository<Item, String>,
    QueryDslPredicateExecutor<Item> , QuerydslBinderCustomizer<QItem> {

  default void customize(QuerydslBindings bindings, QItem data) {

    bindings.bind(String.class).all((MultiValueBinding<StringPath, String>) (path, values) -> {
      BooleanBuilder predicate = new BooleanBuilder();
      values.forEach(value -> {
            if (value.startsWith("~") && !value.endsWith("~")) {

              predicate.or(path.endsWithIgnoreCase(value.substring(1)));

            } else if (!value.startsWith("~") && value.endsWith("~")) {

              predicate.or(path.startsWithIgnoreCase(value.substring(0,value.length() - 1)));

            } else if (value.startsWith("~") && value.endsWith("~")) {

              predicate.or(path.containsIgnoreCase(value.substring(1, value.length() - 1)));

            } else {
              predicate.or(path.equalsIgnoreCase(value));
            }
          }
      );
      return predicate;
    });
  }
}

0 个答案:

没有答案