Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a generic REST query language for JPA, spring-data, spring-data-rest

Requirements

  • generic query language that can be used in GET requests to collection resources in REST api to filter the set of resources returned
  • queries passed in via a "standard" query language and sent over HTTP as request parameter(s) - .e.g /someresource?query=...... or /someresource?a.b.c=2
  • SQL queries constructed at runtime on the server
  • tight integration with jpa, spring-data, spring-data-rest - the less code the better.
  • nested resource and attribute paths available for queries
  • support for complex operands - EQUALS, GREATER_THAN, LESS_THAN, NEGATION, LIKE, AND, OR, NOT, IN

E.g. resourceA.attribute1 = "CAT" AND resourceA.subResourceB.attribute2 >= 42 AND resourceA.attribute3 IN ("WHIZ","BANG")

I've investigated four solutions - each getting closer to the goal. Is there some other solution I haven't found or is there no such complete solution out of the box - is the answer to build upon the "REST query language with RSQL" outlined below?

1) spring-data-rest queries

There is plenty of support in spring data for developing complex queries in code, however this requires the developer to be aware of the structure of queries beforehand and to construct the code accordingly. https://docs.spring.io/spring-data/rest/docs/current/reference/html/#repository-resources.query-method-resource

2) spring-data, spring-data-rest, query-dsl

http://www.baeldung.com/rest-api-search-querydsl-web-in-spring-data-jpa

+ve An excellent fit - thoroughly capable solution with almost zero coding out of the box

+ve deeply nested queries can be constructed and the server generates correct SQL on the fly.

-ve the only operator is EQUALS '=' in order to apply additional operators you need to implement QuerydslBinderCustomizer instances which once again requires the server code to be aware of the complexity of the query in advance.

https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/querydsl/binding/QuerydslBinderCustomizer.html

3) Baeldung - "building a rest query language"

http://www.baeldung.com/spring-rest-api-query-search-language-tutorial

  • basic operations (EQUALS, GREATER_THAN, LESS_THAN)
    http://www.baeldung.com/rest-api-search-language-spring-data-querydsl
  • advanced operations ((EQUALS, GREATER_THAN, LESS_THAN, NEGATION, LIKE) http://www.baeldung.com/rest-api-query-search-language-more-operations
  • OR operator http://www.baeldung.com/rest-api-query-search-or-operation

+ve - getting closer to generic query language

-ve - feels like a demo / POC

4) REST Query Language with RSQL

http://www.baeldung.com/rest-api-search-language-rsql-fiql

+ve - feels like a more complete Query language & associated parser

-ve - unsure of spring integration

like image 455
Nathan Coast Avatar asked May 10 '18 12:05

Nathan Coast


People also ask

What is difference between Jparepository and CrudRepository?

Crud Repository is the base interface and it acts as a marker interface. JPA also provides some extra methods related to JPA such as delete records in batch and flushing data directly to a database. It provides only CRUD functions like findOne, saves, etc. JPA repository also extends the PagingAndSorting repository.

What is Spring data JPA specification?

SPring Data Jpa Specifications helps us to create dynamic queries based on the requirement at run time. Spring Data Jpa Specifications allows a combination of the attributes or properties of a domain or entity class and creates a query.

What is rest JPA?

This repository is an interface that lets you perform various operations involving Person objects. It gets these operations by extending the PagingAndSortingRepository interface that is defined in Spring Data Commons. At runtime, Spring Data REST automatically creates an implementation of this interface.

What is the Spring data rest?

Spring Data REST builds on top of Spring Data repositories, analyzes your application's domain model and exposes hypermedia-driven HTTP resources for aggregates contained in the model.


1 Answers

There is no generic REST query language for JPA. What you've identified seems to be what is out there, however the low recent activity on querydsl and rsql suggests that you should use caution when adopting them. You will most likely have to support additional changes yourself by forking the projects, especially 5 years from now when for sure the author has moved on to other things.

Some other interesting links:

5) Use annotations to dynamically build queries

  • https://blog.tratif.com/2017/11/23/effective-restful-search-api-in-spring
  • maps annotations on a controller method via Spring's argument resolvers to JPA criteria specifications
  • places SQL concepts like joins in the web controller, which leaks abstractions
  • should be implemented at a service level not a controller level
like image 104
dukethrash Avatar answered Jan 12 '23 13:01

dukethrash