Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I filter data in a restful way using Spring?

Tags:

As the title says.

I basically would love to do requests like

/api/todos/?completed=eq.true&created_at=lt.1486462109399 

Is there any ready spring way of achieving such? Something akin to the Page/Pageable mechanism would be great.

If there is none I think I could implement it using Hibernate Criteria Queries & Argument Re-solvers. Basically allowing me to write my controllers like

 @GetMapping  public ResponseEntity<Page<TodoDTO>> listAll(Criteria criteria, Pageable pageable)   {         Page<Todo> todos = todoService.listAll(criteria, pageable)         ...  } 

A custom Argument resolver would be responsible for turning the query string into a Criteria. Not quite sure yet how I would handle it within the service but that's the direction in which I would try to implement this.

Would that be a good approach? Any recommendations? (All assuming there are no ready mechanism for such already).

Your help is much appreciated.

like image 931
Dawid Avatar asked Feb 07 '17 10:02

Dawid


People also ask

What ways can we add filter to a spring boot application?

There are three ways to add your filter, Annotate your filter with one of the Spring stereotypes such as @Component. Register a @Bean with Filter type in Spring @Configuration. Register a @Bean with FilterRegistrationBean type in Spring @Configuration.

How filters are used in Spring Web?

Spring Security maintains a filter chain internally where each of the filters has a particular responsibility and filters are added or removed from the configuration depending on which services are required. The ordering of the filters is important as there are dependencies between them.


1 Answers

Another option to build a fluent query API is to use a RSQL parser. RSQL is a query language for parametrized filtering of entries in RESTful APIs. Follow this article and your API would be able to handle URLs like:

http://localhost:8080/users?search=firstName==jo*;age<25 

Sample controller:

@RestController @RequestMapping(value = "/users") public class UserController {      @Autowired     private UserRepository repo;      @GetMapping     public List<User> findAllByRsql(@RequestParam(value = "search") String search) {         Node rootNode = new RSQLParser().parse(search);         Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());         return repo.findAll(spec);     }  } 
like image 141
naXa Avatar answered Oct 11 '22 01:10

naXa