Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mschout/spring-sliced-resources
Library to add support for SlicedModel and SlicedResourcesAssembler for Spring Applications
https://github.com/mschout/spring-sliced-resources
Last synced: about 1 month ago
JSON representation
Library to add support for SlicedModel and SlicedResourcesAssembler for Spring Applications
- Host: GitHub
- URL: https://github.com/mschout/spring-sliced-resources
- Owner: mschout
- License: apache-2.0
- Created: 2022-10-05T17:41:21.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2022-10-05T18:59:28.000Z (over 2 years ago)
- Last Synced: 2023-05-25T23:32:36.723Z (over 1 year ago)
- Language: Java
- Size: 90.8 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# Spring SlicedModel and SlicedResourceAssembler Support
This is a plugin for [Spring](https://spring.io) applications that enhances support for converting
[Slice](https://docs.spring.io/spring-data/commons/docs/current/api/org/springframework/data/domain/Slice.html) objects
to models that work with Spring HATEOAS. The main reason to do this instead of just using [Page]() collections is that
Slice's do not need to do a `SELECT COUNT(*)` query to determine the total number of elements. Instead, Slice's only
need to know if there is a next element after the current slice or not. When working with larger datasets, using
Slice's can be a huge performance improvement.If you were using `Page` collections, your controller code might look something like this:
```java
// Spring injects via constructor
private final PagedResourcesAssembler pagedResourcesAssembler;@GetMapping
public PagedModel> someControllerMethod(Pageable pageable){
Page people = personRepository.findAll(pageable);
return pagedResourceAssembler.toModle(people);
}
```If you wanted to instead use Slice's, it would look something like this:
```java
// Spring injects via constructor
private final SlicedResourcesAssembler slicedResourcesAssembler;@GetMapping
public SlicedModel> someControllerMethod(Pageable pageable){
Slice people = personRepository.findSlice(pageable);return slicedResourceAssembler.toModle(people);
}
```Unfortunately, at this time, Spring does not have support for the sliced version because:
- There is no `SlicedModel` or equivalent available in Spring HATEOAS
- There is no `SlicedModelAssembler` available in Spring Data Commons and as a result no way for spring to auto inject one for you.This package bridges the gap for you.
# Usage
Add this library as a dependency to your application. I've published this to
Maven Central so no special repositories needed or anything.For gradle:
```java
implementation "io.github.mschout:spring-sliced-resources:${latestVersion}"
```Then in your Spring Boot application class, just import slice support:
```java
package your.spring.app;// imports
@SpringBootApplication
@Import(SpringSlicedResources.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
```And that's it. Spring will be able to auto-inject `SlicedResourcesAssembler` automatically for you, and you will
have `SlicedModel` available.Hopefully eventually this gets merged upstream into core eventually. I do have PR's open to submit this work upstream.
We'll see what happens.