Class RsqlSpecificationArgumentResolver

java.lang.Object
in.co.akshitbansal.springwebquery.RsqlSpecificationArgumentResolver
All Implemented Interfaces:
org.springframework.web.method.support.HandlerMethodArgumentResolver

public class RsqlSpecificationArgumentResolver extends Object implements org.springframework.web.method.support.HandlerMethodArgumentResolver
Spring MVC HandlerMethodArgumentResolver that resolves controller method parameters annotated with RsqlSpec into Spring Data JPA Specifications.

This resolver enables transparent usage of RSQL queries in controller methods. When a request contains an RSQL query parameter, the resolver:

  1. Parses the RSQL query string into an AST
  2. Validates the AST against the target entity using ValidationRSQLVisitor
  3. Converts the validated query into a Specification using RSQLJPASupport

If the RSQL query parameter is missing or blank, the resolver returns an unrestricted Specification (equivalent to no filtering).

Example controller usage:


 @GetMapping("/users")
 public List<User> search(
     @RsqlSpec(entityClass = User.class) Specification<User> spec
 ) {
     return userRepository.findAll(spec);
 }
 

This resolver must be registered via WebMvcConfigurer to be active.

See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    RsqlSpecificationArgumentResolver(Set<RsqlOperator> defaultOperators, Set<? extends RsqlCustomOperator<?>> customOperators)
    Creates a new RsqlSpecificationArgumentResolver with the specified operators.
  • Method Summary

    Modifier and Type
    Method
    Description
    org.springframework.data.jpa.domain.Specification<?>
    resolveArgument(@NonNull org.springframework.core.MethodParameter parameter, org.springframework.web.method.support.ModelAndViewContainer mavContainer, @NonNull org.springframework.web.context.request.NativeWebRequest webRequest, org.springframework.web.bind.support.WebDataBinderFactory binderFactory)
    Resolves the controller method argument into a Specification.
    boolean
    supportsParameter(org.springframework.core.MethodParameter parameter)
    Determines whether this resolver supports the given method parameter.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • RsqlSpecificationArgumentResolver

      public RsqlSpecificationArgumentResolver(Set<RsqlOperator> defaultOperators, Set<? extends RsqlCustomOperator<?>> customOperators)
      Creates a new RsqlSpecificationArgumentResolver with the specified operators.
      Parameters:
      defaultOperators - set of default RSQL operators to support
      customOperators - set of custom RSQL operators to support
  • Method Details

    • supportsParameter

      public boolean supportsParameter(org.springframework.core.MethodParameter parameter)
      Determines whether this resolver supports the given method parameter.

      A parameter is supported if:

      • It is assignable to Specification
      • It is annotated with RsqlSpec
      Specified by:
      supportsParameter in interface org.springframework.web.method.support.HandlerMethodArgumentResolver
      Parameters:
      parameter - the method parameter to check
      Returns:
      true if this resolver supports the parameter
    • resolveArgument

      public org.springframework.data.jpa.domain.Specification<?> resolveArgument(@NonNull @NonNull org.springframework.core.MethodParameter parameter, org.springframework.web.method.support.ModelAndViewContainer mavContainer, @NonNull @NonNull org.springframework.web.context.request.NativeWebRequest webRequest, org.springframework.web.bind.support.WebDataBinderFactory binderFactory)
      Resolves the controller method argument into a Specification.

      The RSQL query is read from the request parameter defined by RsqlSpec.paramName(). The query is then parsed, validated, and converted into a JPA Specification.

      Specified by:
      resolveArgument in interface org.springframework.web.method.support.HandlerMethodArgumentResolver
      Parameters:
      parameter - the method parameter to resolve
      mavContainer - the model and view container
      webRequest - the current web request
      binderFactory - the data binder factory
      Returns:
      a Specification representing the RSQL query, or an unrestricted Specification if the query is absent
      Throws:
      QueryException - if the RSQL query is invalid or violates RsqlFilterable constraints