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:
- Parses the RSQL query string into an AST
- Validates the AST against the target entity using
ValidationRSQLVisitor - Converts the validated query into a
SpecificationusingRSQLJPASupport
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
ConstructorsConstructorDescriptionRsqlSpecificationArgumentResolver(Set<RsqlOperator> defaultOperators, Set<? extends RsqlCustomOperator<?>> customOperators) Creates a new RsqlSpecificationArgumentResolver with the specified operators. -
Method Summary
Modifier and TypeMethodDescriptionorg.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 aSpecification.booleansupportsParameter(org.springframework.core.MethodParameter parameter) Determines whether this resolver supports the given method parameter.
-
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 supportcustomOperators- 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:
supportsParameterin interfaceorg.springframework.web.method.support.HandlerMethodArgumentResolver- Parameters:
parameter- the method parameter to check- Returns:
trueif this resolver supports the parameter
- It is assignable to
-
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 aSpecification.The RSQL query is read from the request parameter defined by
RsqlSpec.paramName(). The query is then parsed, validated, and converted into a JPASpecification.- Specified by:
resolveArgumentin interfaceorg.springframework.web.method.support.HandlerMethodArgumentResolver- Parameters:
parameter- the method parameter to resolvemavContainer- the model and view containerwebRequest- the current web requestbinderFactory- the data binder factory- Returns:
- a
Specificationrepresenting the RSQL query, or an unrestricted Specification if the query is absent - Throws:
QueryException- if the RSQL query is invalid or violatesRsqlFilterableconstraints
-