实体仓库扩展了JpaRepository<Entity, Id>和JpaSpecificationExecutor<Entity>,EntitySpec扩展了Specification<Entity>
P粉653045807
P粉653045807 2024-04-06 17:11:03
0
1
460

这是我的端点 http://localhost:8080/country/all 回复所有国家都可以

当我想过滤它们时返回未找到 国家为空并返回异常 throw new RecordNotFoundException("无效请求,无数据返回"); 在我的数据库中有一条记录 县_代码 国家 KM科摩罗

这是我的 json 负载

{
     "country_code":["KM"]
    }
public interface CountryRepo extends JpaRepository<Country, Id>, JpaSpecificationExecutor<Country> {

    }
    public final class CountrySpecs<T> implements Specification<T> {
    
    final private SearchCriteria criteria;
    
    public CountrySpecs(SearchCriteria searchCriteria) {
        this.criteria = searchCriteria;
    }

        @Override
        @Nullable
        public Predicate toPredicate(Root<T> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
        switch (criteria.getOperation()) {
            case ":":
              if (root.get(criteria.getKey()).getJavaType() == String.class) {
                return builder.like(
                        root.<String>get(criteria.getKey()),
                        "%" + criteria.getValue() + "%");
              } else {
                return builder.equal(root.get(criteria.getKey()),
                        criteria.getValue());
              }
            case "=":
              return builder.equal(root.get(criteria.getKey()),
                      criteria.getValue());
            case "in":
              return builder.in(root.get(criteria.getKey())).value(criteria.getValue());
            default:
              return null;
       }
       }
    }
     @GetMapping("/all")
     public ResponseEntity<Map<String, Object>> getAll(
         @Valid @RequestBody Map<String, Object> request) throws Exception {
         List<Country> countries = new ArrayList<>();
 
         int page = (request.get("page") != null) ? (int) request.get("page") : 1;
         int size = (request.get("size") != null) ? (int) request.get("size") : DEFAULT_SIZE;

        // page-1 mi da la possibilità di scrivere nel payload page=1
        Pageable pageable = PageRequest.of(
           page,
          size);
        Page<Country> pageCountries = null;

       /* Validazione */

       /* filter */
       if (request.keySet().size() > 0) {
          CountrySpecs<Country> countrySpecs = null;
          if (request.keySet().contains("country_code")) {
             List<String> list = (List<String>) request.get("country_code");
             /* paginazione filtrata */
             countrySpecs = new CountrySpecs<>(new SearchCriteria("country_code", "in", list));
             pageCountries = repository.findAll(
               countrySpecs,
               pageable);
             countries = pageCountries.getContent();
          }
      } else {
        pageCountries = repository.findAll(pageable);
        countries = pageCountries.getContent();
      }

      if (countries.isEmpty()) {
        // la tabella interrogata non ha dati
        throw new RecordNotFoundException("无效请求,无数据返回");
      }

      Map<String, Object> obj = new TreeMap<>();
      // aggiunta delle informazioni sulla paginazione
      obj.put("countries", countries);
      obj.put("currentPage", pageCountries.getNumber());
      obj.put("totalItems", pageCountries.getTotalElements());
      obj.put("totalPages", pageCountries.getTotalPages());

     ResponseEntity<Map<String, Object>> response = new ResponseEntity<>(
        obj,
        HttpStatus.OK);
       return response;
    }

谢谢Rashin,我已经改进了代码,但这个问题仍然存在! 希望能找到解决办法。

重置代码后,它正在工作......

P粉653045807
P粉653045807

全部回复(1)
P粉198814372

雷雷

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!