Java convert list to page

mauriziofranco###blog

Suppose you’re retrieving via typedQuery some data.
You probably are getting a List via getResultList() method(TypedQuery class).
Now you need to convert into into a Page result, preserving pagination, obtained with page, size(, ecc.) parameters.
In my case I solve it with this:
[. ]
int pageNumber = 1 ;
int size = 1 ;
PageRequest page = new PageRequest(pageNumber, size);
List articlesList = articleTypedQuery.getResultList();
int start = page.getOffset();
int end = (start + page.getPageSize()) > articlesList .size() ? articlesList .size() : (start + page.getPageSize());
int totalRows = articlesList .size();
Page pageToReturn = new PageImpl(articlesList .subList(start, end), page, totalRows);

return pageToReturn;

153 comments:

Thanks for the solution. I’ve updated it so that there’s an extra check:
pageable.getOffset() >= list.size() => empty page.

private Page toPage(List list, Pageable pageable) if (pageable.getOffset() >= list.size()) return Page.empty();
>
int startIndex = (int)pageable.getOffset();
int endIndex = (int) ((pageable.getOffset() + pageable.getPageSize()) > list.size() ?
list.size() :
pageable.getOffset() + pageable.getPageSize());
List subList = list.subList(startIndex, endIndex);
return new PageImpl(subList, pageable, list.size());
> Reply Delete

The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Project Centers in Chennai for CSE.

Читайте также:  Ftp function in php

Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai Reply Delete

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert List users to Page in spring data mongo Pagination #248

convert List users to Page in spring data mongo Pagination #248

Comments

I am really struggling to convert List users to Page in spring data mongo? Note Page is an API from org.springframework.data.domain.Page ;

I am using Pagination of Spring Data Mongo, so I need to sent Page and not the List . Please help me.

The text was updated successfully, but these errors were encountered:

Thank you. You’re just a great . Do you know how we can make @PageableDefault(size = 20, page = 0) Pageable pageable configurable through properties file?

So, the pagination is driven by the client (in our case, the UI) — not by the API. In the API, both the size and the page are parameters, so whatever the client asks for, that’s what the API will return.
If you need these values, you need to make sure that’s how the UI sends out the HTTP request.

Right, thank you. But API also provide the way to have defaults if values don’t come from UI. so, I was looking to make those default values as configurable ?

If you’re thinking of findAllRedirectToPagination — yes, these are hardcoded. The way to make open that up is to:

  • add a new properties file called web.properties
  • import that in via a @PropertySource in WebConfig
  • add the new property there: web.pagination.size
  • inject that either via the Environment or just with @Value in the abstract controller
  • use it to replace the hard-coded values

Thanks for idea. I will try for it, but could you please provide sudo code ? Because it’s not working at my end and giving error: The value for annotation attribute PageableDefault.size must be a constant expression
public final int pageIndex = Integer.parseInt(Properties.getProperty(«pagination.page.index»)); public final int pageSize = Integer.parseInt(Properties.getProperty(«.pagination.page.size»)); @PageableDefault(size = pageIndex, page = pageSize ) Pageable pageable

Let’s get these changes into a branch and I’ll have a look. Let me know what that branch is.

I was facing an issue in developing Pagination through UI (Angular JS). (I am doing some customization), AJ API pagination starts from Index=1 by default, so we need mechanism if we get page=1, then Pageable should start from page=0 to get data from DB and size=10 as fixed. So I research we can do it using following config, I did refere link — . I am still researching for how effectively we can unit test in junit?

A couple of things here. First — why not use Java config?
Second — yes, you can configure that just fine via the defaults. Testing it can be done from outside the API, with a live test. That should enable you to quite easily test these defaults. You can of course write a simple integration test, or Spring MVC specific tests as well — to get to the same result here.
Hope that helps. Cheers,
Eugen.

I want to do pagination in mybatis using the default values of pagination not by giving value for the page size. Is there any approach for it . and also how can I convert List into Page using mybatis.Kindly guide me for the same

So, for this kind of technical question not strictly related to any article, the best way to ask if over on StackOverflow — then email me the link and I’ll have a look.

Hi eugenp, Please check my code and respose. Problem is _embedded is not coming in response. Please help me how to convert content to _embedded

@RequestMapping(value = «/technicalSearch», method = RequestMethod.POST, consumes=»application/json»)
HttpEntity persons(Pageable pageable,
PagedResourcesAssembler assembler, @requestbody List techSearchParam) List result=repo.searchByCriteria(techSearchParam);
Page pg=new PageImpl(result, new PageRequest(0, 50),result.size());
ResponseEntity pg1=new ResponseEntity<>(assembler.toResource(pg), HttpStatus.OK);
return pg1;

«links» : [ «rel» : «self»,
«href» : «http://localhost:8090/quote_ws/technicalSearch?page=0&size=50»
> ],
«content» : [ «createdBy» : «vivek.rathor@ncs-india.com»,
«createdAt» : «2018-01-19T10:37:12.276+0000»

> ],
«page» : «size» : 50,
«totalElements» : 4,
«totalPages» : 1,
«number» : 0
>
>

Hey @omm7029 — this looks like an interesting question, but it’s also unrelated to any of the modules here, so you’ll have to go to StackOverflow with this one.
Feel free to email me the link, in case I get to it before the community is able to.
Cheers,
Eugen.

to convert from List to Pagination, i used the solution what you guys discussed but its not working, its returning all the data

How about to convert from Pagination to List?

I tried the suggestion above but it seems that the totalElements, totalPages values are wrong.
It displays all the results. numberOfElements value is not changing. while totalElements is varying.

lets say the number of results is 7.

current page is 1 and size is 5..
«first»: true,
«last»: false,
«number»: 0,
«numberOfElements»: 7,
«size»: 5,
«totalElements»: 7,
«totalPages»: 2

current page is 2 and size is 5..
«first»: false,
«last»: false,
«number»: 1,
«numberOfElements»: 7,
«size»: 5,
«totalElements»: 12,
«totalPages»: 3

current page is 3 and size is 5..
«first»: false,
«last»: false,
«number»: 2,
«numberOfElements»: 7,
«size»: 5,
«totalElements»: 17,
«totalPages»: 4

current page is 4 and size is 5..
«first»: false,
«last»: false,
«number»: 3,
«numberOfElements»: 7,
«size»: 5,
«totalElements»: 22,
«totalPages»: 5

It is really acting weird. below is the code used

 public Page getOwnedTicketList(Pageable pageable) throws IOException < User user = getCurrentUser(); Listtickets = m_ticketRepository.getTicketsOwned(user); Page page = new PageImpl<>(tickets, new PageRequest(pageable.getPageNumber(), pageable.getPageSize(), pageable.getSort()), tickets.size()); return page; > 

Источник

Оцените статью