Java Experience

110 posts

Java Experience banner
Java Experience

Java Experience

@JavaExpShare

Java developer who just wants to share some experience with community

Russia, Moscow Katılım Ağustos 2019
31 Takip Edilen122 Takipçiler
Java Experience
Java Experience@JavaExpShare·
Pageable object could specify sorting. Sort object have two properties: - Direction for ascending or descending sorting - Fields to make sorting on #Java #100DaysOfCode #spring
Java Experience tweet media
English
0
1
2
0
Java Experience
Java Experience@JavaExpShare·
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
English
0
1
2
0
Java Experience
Java Experience@JavaExpShare·
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
English
0
2
6
0
Java Experience
Java Experience@JavaExpShare·
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
Java Experience tweet mediaJava Experience tweet mediaJava Experience tweet media
English
0
0
0
0
Java Experience
Java Experience@JavaExpShare·
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
Java Experience tweet media
English
0
3
7
0
Java Experience
Java Experience@JavaExpShare·
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
Java Experience tweet media
English
0
1
4
0
Java Experience
Java Experience@JavaExpShare·
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
Java Experience tweet media
English
0
2
3
0
Java Experience
Java Experience@JavaExpShare·
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
Java Experience tweet media
English
0
3
3
0
Java Experience
Java Experience@JavaExpShare·
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
Java Experience tweet media
English
0
2
5
0
Java Experience
Java Experience@JavaExpShare·
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
Java Experience tweet media
English
0
1
3
0
Java Experience
Java Experience@JavaExpShare·
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
English
0
5
2
0
Java Experience
Java Experience@JavaExpShare·
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
English
0
0
4
0
Java Experience
Java Experience@JavaExpShare·
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
Java Experience tweet media
English
0
1
5
0
Java Experience
Java Experience@JavaExpShare·
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
Java Experience tweet mediaJava Experience tweet media
English
0
1
3
0
Java Experience
Java Experience@JavaExpShare·
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
Java Experience tweet media
English
0
6
5
0
Java Experience
Java Experience@JavaExpShare·
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
Java Experience tweet mediaJava Experience tweet media
English
0
1
2
0