Pageable object could specify sorting.
Sort object have two properties:
- Direction for ascending or descending sorting
- Fields to make sorting on
#Java#100DaysOfCode#spring
The difference between Pageable responses:
- Page returns page + pages count. To achieve this, additional query count will be executed. So we have small overhead
- Slice returns page and knows whether next slice is available
- List returns just current page
#Java#100DaysOfCode
To effective work with Pageable we extends JpaRepository from #spring data JPA. Now we can just pass Pageable as method parameter inside repository and spring will do pagination and sorting for us.
Pageable can return:
- Page
- Slice
- List
#100DaysOfCode#Java
Now we can annotate parameter inside resource and #Jersey will construct and inject Pageable for us.
Then we can simple pass it into spring data repository that supports pagination and that's all!
#Java#100DaysOfCode#spring
Finally we can construct our Pageable from @QueryParam with help of ValueParamProvider and InjectionManager.
I use @DefaultValue to provide default value for property if none specified.
By default there is no sorting and display first page with 15 items.
#Java#100DaysOfCode
To make #Jersey know about our custom provider we should register it inside main configuration file with help of AbstractBinder.
Here we specify binding and scope.
From now on all the injections inside custom provider will start to work.
#Java#100DaysOfCode
Here the interesting part.
We pass the injection manager into function implementation class, so we can inject our @QueryParam.
Then we can construct Pageable class from them.
#Java#100DaysOfCode
Next we implement ValueParamProvider interface from #Jersey. It has 2 abstract methods:
- getValueProvider() here we check for method parameter and return Function that construct that parameter
-getPriority() is priority for provider execution
#Java#100DaysOfCode
First of all we need to declare our custom annotation.
Here we specify:
- @Target is where we can use this annotation. On methods, on classes etc
- @Retention is retention policy. Indicates whether annotation should be stored inside class and available to JVM
#Java#100DaysOfCode
To make such pagination and sorting for request we will specify 3 @QueryParam. Then construct Pageable. It is ok, but not perfect
Imagine we can write single method parameter as Pageable and Jersey will inject @QueryParam inside.
Let's find out how to do that
#Java#100DaysOfCode
For pagination i will pass 2 parameters:
- page is page number
- size is page size
For sorting:
- sort is sorting field + order
The query string will be like:
/rest/vacancies?page=0&size=5&sort=id+desc
where page, size and sort our @QueryParam values.
#Java#Jersey#100DaysOfCode
Hello everyone!
Hope all of you had good holidays and ready for learning :)
Let's write something cool. As you might know good REST API should support pagination and sorting
With #Jersey we can implement this with @QueryParam and i will show how
#100DaysOfCode#NewYear2020#Java
For @QueryParam in #swagger we can specify array of parameters contains:
- in: PathParam.QUERY
- name
- description
- schema with type, example and default value
#Java#100DaysOfCode
Well documented API is very important.
In #Java we have javadoc to document our code.
It is basically block of comments. Common things we can specify:
- description what is going on
- @param method parameters
- @return what is returned
#100DaysOfCode
You can describe request body parameters with #swagger.
No surprise annotation name is @RequestBody. Inside you can provide:
- description
- is body required or not
- content with @Schema with implementation or @ExampleObject with raw example.
#Java#100DaysOfCode
To describe different parameters for resource, such as path parameters, query parameters, #swagger provides @Parameter annotation:
- in: where this parameter should be: header, query, cookie, path
- name
- description
- required
- examples
#Java#100DaysOfCode
@Operation responses consist of @ApiResponse annotations. Here you can set:
- The HTTP response code
- Basic description for response
- Headers
- Content: it could be array with @ArraySchema, or @Schema with implementation ref or with @ExampleObject
#Java#100DaysOfCode#swagger
Inside @Operation in #swagger we can specify:
- summary: basic description for endpoint
- tags: to group up requests by tags
- request body
- responses: available server responses with codes and meanings
- security: security requirements
#Java#100DaysOfCode
Let's discuss basic annotations in #swagger.
Once again, swagger allows us to describe our API, so others can easily use it. This technic also famous as OpenAPI.
In REST application we have resources. To make it visible to swagger, annotate it with @Operation
#Java#100DaysOfCode