Skip to content
プロジェクト

概要

Dynamically filter JPA entities, Mongo collections, and Java in-memory objects with a user-friendly query syntax. Seamless integration with Spring APIs. Star to support the project! ❤️

Web・アプリ開発

この数値の測り方

計測したスター増加

24時間
0
7日間
0
15日間
0
1か月
履歴を収集中

期間ごとの純増数です。期間は重複しており、日別の値ではありません。未計測の値は空欄です。スター数は注目度の指標であり、品質の保証ではありません。

README.ja.md

オリジナル

Spring Filter

Spring Filter Logo

Maven Central Build License Java 17+

:us: English | :cn: 中文 | :es: Español | :tr: Türkçe

Spring アプリケーション向けの動的クエリフィルタリング。フィルタ式を URL パラメータとして渡し、JPA リポジトリ、MongoDB コレクション、またはメモリ内の Java オブジェクトに適用します。

このライブラリはフィルタ式を抽象構文木にパースし、使用するモジュールに応じて JPA Criteria クエリ、MongoDB クエリ、または Java Predicate に変換します。フィルタビルダーを使ってプログラム的にクエリを構築することもできます。

Spring Boot 4 に対応!以前のバージョンは 2.x.x / 3.x.x ブランチをご覧ください。

エコシステム

JavaScript と TypeScript でも利用可能です:

パッケージ 説明
FilterKit コアライブラリ — 配列のフィルタリング、クエリ構築、式の解析
FilterKit TanStack TanStack Table カラムフィルタ → Spring Filter
FilterKit QueryBuilder react-querybuilder クエリ → Spring Filter
FilterKit Prisma フィルタ式 → Prisma where 句
FilterKit Drizzle フィルタ式 → Drizzle where 句

例(ライブデモ)

/search?filter= average(ratings) > 4.5 and brand.name in ['audi', 'land rover'] and (year > 2018 or km < 50000) and color : 'white' and accidents is empty

@Entity public class Car {
  @Id long id;
      int year;
      int km;
  @Enumerated Color color;
  @ManyToOne Brand brand;
  @OneToMany List<Accident> accidents;
  @ElementCollection List<Integer> ratings;
}

ブーリアン、日付、列挙型、関数、エンティティリレーションを処理します。JPA モジュールは Criteria クエリを生成し、MongoDB モジュールは集計パイプラインを生成し、Predicate モジュールはメモリ内オブジェクトをフィルタリングします。

スポンサー

プロジェクトをスポンサーしていただくと、Issue が優先されます。


ixorbv

marcopag90

モジュール

JPA

データベースクエリで直接 JPA エンティティをフィルタリングします。フィルタ式を JPA Criteria API の Specification に変換します。

<dependency>
  <groupId>com.turkraft.springfilter</groupId>
  <artifactId>jpa</artifactId>
  <version>4.1.0</version>
</dependency>
@GetMapping("/cars")
Page<Car> search(@Filter Specification<Car> spec, Pageable page) {
    return repository.findAll(spec, page);
}

リポジトリは JpaSpecificationExecutor を実装する必要があります。SimpleJpaRepository が標準実装です。ページネーションが不要な場合は Pageable 引数を削除してください。

JPA ネイティブクエリ

@GetMapping("/cars/native")
List<Car> searchNative(@Filter Specification<Car> spec) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<Car> query = cb.createQuery(Car.class);
    Root<Car> root = query.from(Car.class);

    query.where(spec.toPredicate(root, query, cb));

    return entityManager.createQuery(query).getResultList();
}

JPA プロジェクション

@GetMapping("/cars/summary")
List<CarSummary> searchProjection(@Filter Specification<Car> spec) {
    CriteriaBuilder cb = entityManager.getCriteriaBuilder();
    CriteriaQuery<CarSummary> query = cb.createQuery(CarSummary.class);
    Root<Car> root = query.from(Car.class);

    query.select(cb.construct(CarSummary.class,
        root.get("brand").get("name"),
        cb.count(root)));
    query.where(spec.toPredicate(root, query, cb));
    query.groupBy(root.get("brand").get("name"));

    return entityManager.createQuery(query).getResultList();
}

MongoDB

Spring Data MongoDB クエリを使用して MongoDB ドキュメントをフィルタリングします。

<dependency>
  <groupId>com.turkraft.springfilter</groupId>
  <artifactId>mongo</artifactId>
  <version>4.1.0</version>
</dependency>
@GetMapping("/cars")
Page<Car> search(@Filter(entityClass = Car.class) Query query, Pageable page) {
    return mongoTemplate.find(query.with(page), Car.class);
}

MongoRepository との統合

public interface CarRepository extends MongoRepository<Car, String> {
  @Query("?0")
  List<Car> findAll(Document document);

  @Query("?0")
  Page<Car> findAll(Document document, Pageable pageable);
}

@GetMapping("/cars")
Page<Car> search(@Filter(entityClass = Car.class) Document document, Pageable page) {
    return repository.findAll(document, page);
}

Predicate

Java Predicate を使用してメモリ内コレクションをフィルタリングします。データベース不要で、任意の POJO で動作します。

<dependency>
  <groupId>com.turkraft.springfilter</groupId>
  <artifactId>predicate</artifactId>
  <version>4.1.0</version>
</dependency>
@GetMapping("/cars")
List<Car> search(@Filter Predicate<Car> predicate) {
    List<Car> allCars = loadCarsFromCache();
    return allCars.stream()
        .filter(predicate)
        .collect(Collectors.toList());
}

手動変換

@Autowired FilterPredicateConverter converter;

public List<Car> filterCars(List<Car> cars, String filterExpression) {
    FilterPredicate<Car> predicate = converter.convert(filterExpression, Car.class);

    return cars.stream()
        .filter(predicate)
        .collect(Collectors.toList());
}

ユースケース

Predicate モジュールは以下の場合に便利です:

  • メモリ内のキャッシュデータのフィルタリング
  • クライアントに返す前の API レスポンスのフィルタリング
  • データベースなしでのフィルタロジックのテスト
  • 設定オブジェクトや列挙型のフィルタリング
  • メモリ内でのバッチデータ処理
@GetMapping("/cars/cached")
List<Car> searchCached(@Filter Predicate<Car> predicate) {
    return cacheService.getAllCars().stream()
        .filter(predicate)
        .collect(Collectors.toList());
}

@GetMapping("/cars/filter-after-fetch")
List<Car> filterAfterFetch(@Filter Predicate<Car> predicate) {
    List<Car> cars = externalApiClient.fetchAllCars();
    return cars.stream()
        .filter(predicate)
        .collect(Collectors.toList());
}

size 関数のサポート

// コレクションのサイズでフィルタ
GET /cars?filter=size(accidents) > 2
GET /owners?filter=size(cars) : 0

Predicate モジュールは全ての標準演算子と、コレクション、配列、Map、文字列の size() 関数をサポートします。

Filter Builder

フィルタ文字列を手動で書く代わりに、プログラム的にフィルタ式を構築します。

<dependency>
  <groupId>com.turkraft.springfilter</groupId>
  <artifactId>core</artifactId>
  <version>4.1.0</version>
</dependency>
@Autowired FilterBuilder fb;

FilterNode filter = fb.field("year").equal(fb.input(2025))
    .and(fb.field("category").isNull())
    .get();

@Autowired ConversionService cs;
String query = cs.convert(filter, String.class);
// year : 2025 and category is null

複雑なクエリ

FilterNode filter = fb.field("brand.name").in(
    fb.collection(fb.input("audi"), fb.input("bmw"))
).and(
    fb.field("year").greaterThan(fb.input(2020))
        .or(fb.field("km").lessThan(fb.input(50000)))
).get();

関数の使用

@Autowired SizeFunction sizeFunction;

FilterNode filter = fb.function(sizeFunction, fb.field("accidents"))
    .greaterThan(fb.input(2))
    .and(fb.field("year").lessThan(fb.input(2015)))
    .get();

型安全フィルタビルダー

JPA エンティティからコンパイル時に型安全なフィルタビルダーを生成します。各フィールドでの IDE オートコンプリート、型安全な比較、リファクタリング安全性を提供します。

<dependency>
  <groupId>com.turkraft.springfilter</groupId>
  <artifactId>typesafe</artifactId>
  <version>4.1.0</version>
</dependency>

コンパイラプラグインにアノテーションプロセッサを追加:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-compiler-plugin</artifactId>
  <version>3.11.0</version>
  <configuration>
    <annotationProcessorPaths>
      <path>
        <groupId>com.turkraft.springfilter</groupId>
        <artifactId>typesafe-processor</artifactId>
        <version>4.1.0</version>
      </path>
    </annotationProcessorPaths>
  </configuration>
</plugin>

エンティティにアノテーションを付与:

@Entity
@Filterable
public class Car {
    private int year;
    private String model;
    private double price;
    private boolean active;
    @ManyToOne private Brand brand;
}

コンパイル時に CarFilter が生成されます。使用方法:

@Autowired FilterBuilder fb;

FilterNode f = CarFilter.where(fb)
    .year().between(2020, 2025)
    .and()
    .model().startsWith("Audi")
    .and()
    .price().greaterThan(100.0)
    .and()
    .active().isTrue()
    .build();

// 生成結果: year between 2020 and 2025 and model ~ 'Audi%' and price > 100.0 and active : true

サポートされるフィールド型:int、long、double、boolean、String、Date、LocalDate、LocalDateTime、列挙型、コレクション。正しい演算子優先順位で and() / or() チェーンをサポート。@Transient または @JsonIgnore が付与されたフィールドはスキップされます。スーパークラスから継承されたフィールドは含まれます。

OpenAPI/Swagger

@Filter パラメータを持つエンドポイントに Swagger ドキュメントを自動追加します。

<dependency>
  <groupId>com.turkraft.springfilter</groupId>
  <artifactId>openapi</artifactId>
  <version>4.1.0</version>
</dependency>

依存関係を追加するだけです。Swagger UI が自動的に以下を表示します:

  • すべてのフィルタ可能フィールドとその型
  • ネストされたリレーション
  • 列挙値
  • サンプルクエリ
  • 演算子リファレンス
  • 利用可能な関数

JPA、MongoDB、Predicate モジュールに対応。

ページネーション、ソート、フィールド選択

page-sort モジュールはページネーション、ソート、フィールド選択のアノテーションを提供します。

<dependency>
  <groupId>com.turkraft.springfilter</groupId>
  <artifactId>page-sort</artifactId>
  <version>4.1.0</version>
</dependency>

基本的な使い方

@GetMapping("/cars")
Page<Car> search(@Filter Specification<Car> spec, @Pagination Pageable page) {
    return repository.findAll(spec, page);
}

使用例:?page=0&size=20&sort=-year(降順の場合は接頭辞 -)

カスタムパラメータ名

@GetMapping("/cars")
Page<Car> search(
    @Pagination(pageParameter = "p", sizeParameter = "limit", sortParameter = "order") Pageable page) {
    return repository.findAll(page);
}

これで ?p=0&limit=50&order=-year が使用できます。

Sort パラメータ

@GetMapping("/cars")
List<Car> search(@Sort org.springframework.data.domain.Sort sort) {
    return repository.findAll(sort);
}

?sort=-year または ?sort=-year,name を使用。

フィールド選択

@Fields
@GetMapping("/cars")
List<Car> search() {
    return repository.findAll();
}

?fields=id,brand.name,year で指定されたフィールドのみを返します。内部で Jackson のフィルタリングを使用します。

// 特定のフィールドを含める
?fields= id,name,email

// フィールドを除外
?fields= *,-password,-ssn

// ネストされたフィールド
?fields= id,brand.name,brand.country

// ワイルドカード
?fields= user.*

ルートスコープ

root を使用して、ラッパーパス内でフィールド選択をスコープします。ページネーションメタデータ(totalElements、totalPages)を常に含めつつ、content 内のフィールドのみをフィルタしたいページネーション付きレスポンスに便利です:

@Fields(root = "content")
@GetMapping("/cars")
Page<Car> search() {
    return repository.findAll(pageable);
}

これで ?fields=id,brand.name,year は content 内のプロパティのみにマッチし、content 外のメタデータフィールドは常にレスポンスに含まれます。

組み合わせ例

@Fields
@GetMapping("/cars")
Page<Car> search(
    @Filter Specification<Car> spec,
    @Pagination Pageable page) {
    return repository.findAll(spec, page);
}

すべての機能を同時に使用:

/cars?filter=year>2020&page=0&size=20&sort=-year&fields=id,brand.name,year

両方の依存関係がある場合、openapi モジュールがこれらのパラメータのドキュメントを自動生成します。

フロントエンド統合

JavaScript/TypeScript でフィルタ式を構築するには FilterKit を使用します。Spring Filter と全く同じ式構文と AST を共有します。

import { build, stringify } from '@turkraft/filterkit';

const query = build()
  .field('year').between(2020, 2025)
  .and(build().field('brand.name').in(['audi', 'bmw']))
  .get();

fetch(`/api/cars?filter=${encodeURIComponent(stringify(query))}`);

FilterKit は TanStack Table と react-querybuilder 向けの React 統合も提供しています。

コミュニティプロジェクト

言語構文

フィールドアクセス

field
field.nested
field.nested.deep

リテラル

123                 // 整数
-321.123            // 小数
true, false         // 真偽値
'text'              // 文字列
'1-01-2023'         // 日付(形式は Spring ConversionService に依存)
'escape \' quote'   // エスケープ文字列

コレクション

[1, 2, 3]
['a', 'b', 'c']
[field, nested.field, 'literal']
[field, ['nested', 'array'], 99]

関数

すべてのモジュールで利用できます:

size(collection)
size(field.collection)
today()

JPA モジュールは、JPA Criteria 式に対応する以下の関数を追加します:

// 算術         abs, neg, sign, ceiling, floor, sqrt, exp, ln, power, mod,
//              sum, diff, prod, quot
// 集約         count, countDistinct, avg, min, max, exists, greatest, least
// 文字列       upper, lower, trim, concat, length, locate, substring
// 日付・時刻   currentDate, currentTime, currentTimestamp,
//              localDate, localTime, localDateTime
// 型変換       toInteger, toDouble, toFloat, toString, toBigDecimal, toBigInteger
// 限定子       any, all, some
upper(name) : 'JOHN'
length(description) > 100
concat(firstName, ' ', lastName) ~ '%smith%'
toInteger(code) > 500
price > all(items.price)

jsonText は JPA 専用かつ PostgreSQL 固有で、jsonb_extract_path_text にコンパイルされます。 文字列を返すため、文字列として比較するか、先に型変換してください:

jsonText(field, 'key')
jsonText(field, 'key1', 'key2', ...)
toInteger(jsonText(field, 'age')) > 18

プレースホルダー

`placeholder_name`

プレースホルダーは、実装したカスタムプレースホルダープロセッサによって解決されます。

演算子

a and b              // 論理積
a or b               // 論理和
a xor b              // 排他的論理和
not a                // 否定
a : b                // 等しい
a ! b                // 等しくない
a > b                // より大きい
a >: b               // 以上
a < b                // より小さい
a <: b               // 以下
a between x and y    // 範囲(両端を含む)
a ~ 'pattern'        // パターンマッチ(ワイルドカードはモジュールごとに異なる。下記参照)
a ~~ 'pattern'       // 大文字小文字を区別しないパターンマッチ
a in [x, y]          // コレクションに含まれる
a not in [x, y]      // コレクションに含まれない
a is null            // null チェック
a is not null        // 非 null チェック
a is empty           // 空チェック(コレクション/文字列)
a is not empty       // 非空チェック

モジュール間のパターンマッチング

~ と ~~ は、モジュールごとに同じようには変換されません。実際に問い合わせるモジュールに 合わせてパターンを記述してください:

パターン内 JPA MongoDB Predicate
% 任意の文字列 % そのもの 任意の文字列
_ 任意の 1 文字 _ そのもの 任意の 1 文字
* % の別名 任意の文字列 * そのもの
ワイルドカードなしのパターン 前後を補い、部分一致 前後を補い、部分一致 値全体と一致する必要がある

そのため name ~ 'John' は JPA と MongoDB では "Johnny" に一致しますが、predicate モジュール では一致しません。また name ~ '%john%' は MongoDB では %john% という文字列そのものを検索 します。MongoDB では * を、JPA では %/_ を使用してください。これを統一することは互換性を 壊す変更となるため、将来のメジャーバージョンに見送られています。

優先順位

括弧を使用して評価順序を制御します:

a and (b or c)
(status : 'active' or status : 'pending') and year > 2020

クエリ例

基本的なフィルタリング

// 単純な等価
?filter= status : 'active'

// 複数条件
?filter= year > 2020 and km < 50000

// 範囲(両端を含む)
?filter= year between 2020 and 2025

// OR 条件
?filter= color : 'red' or color : 'blue'

リレーションの操作

// 関連エンティティのフィールドでフィルタ
?filter= brand.name : 'audi'

// ネストされたリレーション
?filter= brand.manufacturer.country : 'germany'

// リレーションの存在確認
?filter= brand is not null

// 複数のリレーション条件
?filter= brand.name : 'audi' and dealer.city : 'berlin'

コレクション操作

// 値がリストに含まれるか確認
?filter= status in ['active', 'pending', 'review']

// コレクションのサイズ確認
?filter= size(accidents) > 2

// コレクションが空か確認
?filter= accidents is empty

// コレクションが空でないか確認
?filter= ratings is not empty

文字列マッチング

// ワイルドカード付きパターンマッチ(% = 任意の文字, _ = 1文字)
?filter= name ~ '%john%'

// 大文字小文字を区別しないパターンマッチ
?filter= name ~~ 'JOHN'

// 前方一致
?filter= email ~ 'admin%'

// 後方一致
?filter= filename ~ '%.pdf'

// パターンマッチング
?filter= code ~ 'PRD-____-2023'

// 複数パターン
?filter= name ~ ['%john%', '%doe%']

// 大文字小文字を区別しない複数パターン
?filter= name ~~ ['%JOHN%', '%DOE%']

日付フィルタリング

// 日付比較(形式は Spring の設定に依存)
?filter= createdAt > '2023-01-01'

// 日付範囲
?filter= createdAt > '2023-01-01' and createdAt < '2023-12-31'

// 日付範囲(between)
?filter= createdAt between '2023-01-01' and '2023-12-31'

// today() 関数を使用した相対日付
?filter= createdAt > today()

複雑なクエリ

// 優先順位付きのネストされた条件
?filter= (year > 2020 and km < 30000) or (year > 2018 and km < 10000)

// 異なる演算子の組み合わせ
?filter= brand.name in ['audi', 'bmw'] and year > 2020 and accidents is empty and color ! 'white'

// リレーション付きコレクションサイズ
?filter= size(owner.vehicles) > 3 and status : 'active'

Null と空のチェック

// null チェック
?filter= deletedAt is null

// 非 null チェック
?filter= description is not null

// 空コレクション
?filter= tags is empty

// 非空コレクション
?filter= children is not empty

JSON/JSONB フィルタリング

// キーパスで文字列値を抽出
?filter= jsonText(metadata, 'address', 'city') : 'New York'

// 値が存在するか確認
?filter= jsonText(data, 'status') in ['active', 'pending']

// 型キャストと組み合わせ
?filter= toInteger(jsonText(data, 'age')) > 18

// パターンマッチング付きのネストされたキー
?filter= jsonText(metadata, 'address', 'city') ~ '%York%'

サポートされる型

すべてのモジュールで以下を処理します:

  • プリミティブ型(int、long、double、boolean など)
  • 文字列
  • 列挙型
  • 日付(LocalDate、LocalDateTime、Date、Instant など)
  • コレクション(List、Set)
  • 配列
  • エンティティリレーション(@ManyToOne、@OneToMany、@ManyToMany)

日付の解析は Spring の ConversionService を使用します。設定で日付形式を変更できます。

カスタム演算子

FilterInfixOperator、FilterPrefixOperator、FilterPostfixOperator を継承してカスタム演算子を定義します:

@Component
public class ContainsOperator extends FilterInfixOperator {
    public ContainsOperator() {
        super("contains", 5);
    }
}

次に、各モジュールのプロセッサを実装します:

@Component
public class ContainsOperationExpressionProcessor implements
    FilterInfixOperationProcessor<FilterExpressionTransformer, Expression<?>> {

    @Override
    public Class<FilterExpressionTransformer> getTransformerType() {
        return FilterExpressionTransformer.class;
    }

    @Override
    public Class<ContainsOperator> getDefinitionType() {
        return ContainsOperator.class;
    }

    @Override
    public Expression<?> process(FilterExpressionTransformer transformer,
                                  InfixOperationNode source) {
        // 実装
    }
}

自動設定または手動設定で演算子を登録します。

カスタム関数

FilterFunction を継承してカスタム関数を定義します:

@Component
public class LowerFunction extends FilterFunction {
    public LowerFunction() {
        super("lower");
    }
}

各モジュールのプロセッサを実装します:

@Component
public class LowerFunctionExpressionProcessor implements
    FilterFunctionProcessor<FilterExpressionTransformer, Expression<?>> {

    @Override
    public Class<FilterExpressionTransformer> getTransformerType() {
        return FilterExpressionTransformer.class;
    }

    @Override
    public Class<LowerFunction> getDefinitionType() {
        return LowerFunction.class;
    }

    @Override
    public Expression<?> process(FilterExpressionTransformer transformer,
                                  FunctionNode source) {
        Expression<?> arg = transformer.transform(source.getArgument(0));
        return transformer.getCriteriaBuilder().lower((Expression<String>) arg);
    }
}

設定

デフォルトパラメータ名

デフォルトでは、フィルタは filter クエリパラメータから読み取られます。上書きするには:

@GetMapping("/cars")
List<Car> search(@Filter(parameter = "q") Specification<Car> spec) {
    return repository.findAll(spec);
}

これで ?filter=year > 2020 の代わりに ?q=year > 2020 が使用できます。

信頼できない入力の制限

フィルタ式は通常クエリパラメータからそのまま渡されるため、信頼できない入力です。2 つの制限が 適用されます。

ネストの深さは全体で上限が設けられています。上限を超えた式は、リクエストスレッドのスタックを 使い果たすのではなく InvalidSyntaxException として拒否されます:

# デフォルト: 500。0 にするとチェックを無効化します
springfilter.core.max_nesting_depth=500

式の長さはデフォルトでは制限されません。公開エンドポイントではパラメータごとに設定してください:

@GetMapping("/cars")
List<Car> search(@Filter(maxLength = 2000) Specification<Car> spec) {
    return repository.findAll(spec);
}

@Sort と @Pagination にはすでに既定値があります(maxFields = 10、maxSize = 100)。

エラーハンドリング

InvalidSyntaxException には @ResponseStatus(BAD_REQUEST) が付いているため、不正な式は追加の 設定なしで 400 を返します。レスポンスボディを制御するにはハンドラを定義してください:

@ExceptionHandler(InvalidSyntaxException.class)
ResponseEntity<String> onInvalidFilter(InvalidSyntaxException e) {
    return ResponseEntity.badRequest().body("不正なフィルタ: " + e.getMessage());
}

その他の入力ミスは、マッピングしない限り通常の実行時例外のままデフォルトの 500 ハンドラに 到達します。未知の関数やプレースホルダーには UnsupportedOperationException、未知のフィールド、 引数の個数の誤り、フィールドの型に変換できない値には IllegalArgumentException が発生します。

MongoDB のエンティティクラス

MongoDB ではエンティティクラスの明示的な指定が必要です:

@GetMapping("/cars")
List<Car> search(@Filter(entityClass = Car.class) Query query) {
    return mongoTemplate.find(query, Car.class);
}

オプショナルフィルタ

Optional を使用してフィルタパラメータの欠落を処理します:

@GetMapping("/cars")
List<Car> search(@Filter Optional<Specification<Car>> spec) {
    return repository.findAll(spec.orElse(null));
}

プログラム的フィルタ適用

Spring MVC アノテーションなしでフィルタを適用します:

@Autowired FilterSpecificationConverter jpaConverter;
@Autowired FilterQueryConverter mongoConverter;
@Autowired FilterPredicateConverter predicateConverter;

public void manualFiltering() {
    // JPA
    Specification<Car> spec = jpaConverter.convert("year > 2020", Car.class);
    List<Car> jpaCars = repository.findAll(spec);

    // MongoDB
    Query query = mongoConverter.convert("year > 2020", Car.class);
    List<Car> mongoCars = mongoTemplate.find(query, Car.class);

    // Predicate
    FilterPredicate<Car> predicate = predicateConverter.convert("year > 2020", Car.class);
    List<Car> filteredCars = allCars.stream().filter(predicate).collect(Collectors.toList());
}

Spring 設定

Spring Filter は型変換に Spring の ConversionService を使用します。日付形式や列挙型の処理をカスタマイズするには:

@Configuration
public class ConversionConfig {

    @Bean
    public ConversionService conversionService() {
        DefaultConversionService service = new DefaultConversionService();
        service.addConverter(new StringToLocalDateConverter());
        return service;
    }
}

高度な機能:ParseContext

ParseContext を使用すると、解析中にフィルタ式をインターセプトして変更できます。フィールドマッピングとノードマッピングの2つのフックを提供します。

フィールドエイリアス

API のフィールド名をデータベースのフィールド名にマッピングします:

@Service
public class ProductService {

    @Autowired FilterParser parser;
    @Autowired FilterSpecificationConverter converter;

    public List<Product> search(String filter) {
        ParseContext ctx = new ParseContextImpl(field -> {
            return switch (field) {
                case "price" -> "unitPrice";
                case "category" -> "productCategory.name";
                case "inStock" -> "inventory.quantity";
                default -> field;
            };
        }, null);

        FilterNode node = parser.parse(filter, ctx);
        Specification<Product> spec = converter.convert(node);
        return repository.findAll(spec);
    }
}

これで ?filter=price > 100 のようなクエリがデータベースレベルで自動的に unitPrice > 100 になります。

マルチテナンシー

すべてのクエリにテナントフィルタを自動注入します:

@Service
public class TenantAwareFilterService {

    @Autowired FilterParser parser;
    @Autowired FilterSpecificationConverter converter;
    @Autowired FilterBuilder fb;

    public <T> Specification<T> parse(String filter, Long tenantId) {
        ParseContext ctx = new ParseContextImpl(null, userNode -> {
            FilterNode tenantFilter = fb.field("tenantId").equal(fb.input(tenantId)).get();
            return fb.and(tenantFilter, userNode).get();
        });

        FilterNode node = parser.parse(filter, ctx);
        return converter.convert(node);
    }
}
@GetMapping("/products")
Page<Product> search(@Filter String filter, @AuthenticationPrincipal User user) {
    Specification<Product> spec = tenantService.parse(filter, user.getTenantId());
    return repository.findAll(spec, pageable);
}

ユーザーは status : 'active' とクエリしますが、実際のクエリは tenantId : 123 and status : 'active' になります。

セキュリティフィルタ

ユーザー権限に基づいて行レベルのセキュリティフィルタを注入します:

@Service
public class SecureFilterService {

    @Autowired FilterParser parser;
    @Autowired FilterSpecificationConverter converter;
    @Autowired FilterBuilder fb;

    public Specification<Document> parseSecure(String userQuery, User user) {
        ParseContext ctx = new ParseContextImpl(null, userNode -> {
            if (user.hasRole("ADMIN")) {
                return userNode;
            }

            FilterNode securityFilter = fb.field("ownerId").equal(fb.input(user.getId()))
                .or(fb.field("department").equal(fb.input(user.getDepartment())))
                .get();

            return fb.and(securityFilter, userNode).get();
        });

        FilterNode node = parser.parse(userQuery, ctx);
        return converter.convert(node);
    }
}

一般ユーザーは自動的に自分のドキュメントまたは部門のドキュメントに制限されます。管理者はすべてを見ることができます。

動的フィールドアクセス制御

ユーザーがフィルタできるフィールドを制限します:

public class FieldAccessControlContext implements ParseContext {

    private final Set<String> allowedFields;

    public FieldAccessControlContext(User user) {
        this.allowedFields = user.hasRole("ADMIN")
            ? Set.of("id", "name", "email", "salary", "ssn", "department")
            : Set.of("id", "name", "department");
    }

    @Override
    public UnaryOperator<String> getFieldMapper() {
        return field -> {
            if (!allowedFields.contains(field)) {
                throw new SecurityException("Access denied to field: " + field);
            }
            return field;
        };
    }
}
@GetMapping("/employees")
List<Employee> search(@Filter String filter, @AuthenticationPrincipal User user) {
    ParseContext ctx = new FieldAccessControlContext(user);
    FilterNode node = parser.parse(filter, ctx);
    Specification<Employee> spec = converter.convert(node);
    return repository.findAll(spec);
}

管理者以外のユーザーが ?filter=salary > 50000 を試みると SecurityException が発生します。

論理削除のクエリ書き換え

論理削除されたレコードを自動的に除外します:

@Service
public class SoftDeleteFilterService {

    @Autowired FilterParser parser;
    @Autowired FilterSpecificationConverter converter;
    @Autowired FilterBuilder fb;

    public <T> Specification<T> parseWithSoftDelete(String userQuery) {
        ParseContext ctx = new ParseContextImpl(null, userNode -> {
            FilterNode notDeleted = fb.field("deletedAt").isNull().get();
            return fb.and(notDeleted, userNode).get();
        });

        FilterNode node = parser.parse(userQuery, ctx);
        return converter.convert(node);
    }
}

すべてのクエリに自動的に deletedAt is null が含まれます。

監査ログ

ユーザーコンテキスト付きですべてのフィルタクエリをログに記録します:

@Service
public class AuditingFilterService {

    @Autowired FilterParser parser;
    @Autowired FilterSpecificationConverter converter;
    @Autowired AuditLogger auditLogger;

    public <T> Specification<T> parseWithAudit(String query, User user) {
        ParseContext ctx = new ParseContextImpl(null, node -> {
            auditLogger.log("User {} executed filter: {}", user.getId(), query);
            return node;
        });

        FilterNode node = parser.parse(query, ctx);
        return converter.convert(node);
    }
}

フィールド名の正規化

異なる命名規則を処理します:

public class NormalizingParseContext implements ParseContext {

    @Override
    public UnaryOperator<String> getFieldMapper() {
        return field -> {
            String normalized = CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, field);

            if (normalized.endsWith("_id")) {
                return normalized.substring(0, normalized.length() - 3);
            }

            return normalized;
        };
    }
}
ParseContext ctx = new NormalizingParseContext();
FilterNode node = parser.parse("userId : 123 and userName ~ 'john%'", ctx);

userId を user に、userName を user_name に自動変換します。

複数コンテキストの組み合わせ

複雑なシナリオのために複数の解析コンテキストを連結します:

public class CompositeParseContext implements ParseContext {

    private final List<ParseContext> contexts;

    public CompositeParseContext(ParseContext... contexts) {
        this.contexts = Arrays.asList(contexts);
    }

    @Override
    public UnaryOperator<String> getFieldMapper() {
        return field -> {
            String result = field;
            for (ParseContext ctx : contexts) {
                result = ctx.getFieldMapper().apply(result);
            }
            return result;
        };
    }

    @Override
    public UnaryOperator<FilterNode> getNodeMapper() {
        return node -> {
            FilterNode result = node;
            for (ParseContext ctx : contexts) {
                result = ctx.getNodeMapper().apply(result);
            }
            return result;
        };
    }
}
ParseContext ctx = new CompositeParseContext(
    new FieldAccessControlContext(user),
    new TenantFilterContext(user.getTenantId()),
    new SoftDeleteContext()
);

1回のパスで複数の変換を適用します。

リクエストスコープのコンテキスト

Spring のリクエストスコープを使用してコンテキスト認識の解析を行います:

@Component
@Scope(value = WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.TARGET_CLASS)
public class RequestParseContext implements ParseContext {

    @Autowired HttpServletRequest request;
    @Autowired UserService userService;

    @Override
    public UnaryOperator<FilterNode> getNodeMapper() {
        return node -> {
            User user = userService.getCurrentUser();
            FilterNode tenantFilter = fb.field("tenantId")
                .equal(fb.input(user.getTenantId())).get();
            return fb.and(tenantFilter, node).get();
        };
    }
}
@GetMapping("/products")
Page<Product> search(@Filter String filter) {
    ParseContext ctx = applicationContext.getBean(RequestParseContext.class);
    FilterNode node = parser.parse(filter, ctx);
    Specification<Product> spec = converter.convert(node);
    return repository.findAll(spec, pageable);
}

コンテキストは自動的に現在のリクエストのユーザー情報を使用します。

テスト

Filter Builder を使用したユニットテスト

@Test
void testFilterBuilder() {
    FilterNode filter = fb.field("year").greaterThan(fb.input(2020)).get();
    Specification<Car> spec = jpaConverter.convert(filter);

    List<Car> result = repository.findAll(spec);

    assertTrue(result.stream().allMatch(car -> car.getYear() > 2020));
}

統合テスト

@SpringBootTest
@AutoConfigureMockMvc
class FilterIntegrationTest {

    @Autowired MockMvc mockMvc;

    @Test
    void testFilter() throws Exception {
        mockMvc.perform(get("/cars?filter=year > 2020"))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$[*].year").value(everyItem(greaterThan(2020))));
    }
}

Predicate のテスト

@Test
void testPredicateFiltering() {
    List<Car> cars = Arrays.asList(
        new Car("Audi", 2021),
        new Car("BMW", 2019),
        new Car("Mercedes", 2022)
    );

    FilterPredicate<Car> predicate = predicateConverter.convert("year > 2020", Car.class);
    List<Car> filtered = cars.stream().filter(predicate).collect(Collectors.toList());

    assertEquals(2, filtered.size());
}

パフォーマンス

JPA

JPA モジュールは最適化された Criteria クエリを生成します。データベースがフィルタリングを実行するため、パフォーマンスはネイティブ SQL クエリと同等です。フィルタ対象のフィールドに適切なインデックスを作成してください。

MongoDB

MongoDB モジュールは集計パイプラインを生成します。パフォーマンスはインデックスに依存します。MongoDB の explain plan でクエリをプロファイリングしてください。

Predicate

Predicate モジュールはメモリ内でフィルタリングします。パフォーマンスは O(n) で、n はコレクションのサイズです。以下に適しています:

  • 小規模なコレクション
  • キャッシュデータ
  • 既に読み込まれているコレクション
  • テスト

大規模なデータセットの場合は、JPA または MongoDB モジュールを使用してデータベースレベルでフィルタリングしてください。

コントリビューション

プルリクエスト歓迎。フォーマットには Google Java Style を使用してください。

コントリビューター

記事

ライセンス

MIT License - LICENSE ファイルを参照してください。

関連プロジェクトを探す

読み込み中…