简体   繁体   中英

Return Page<E> in Spring data JPA empty

@RestController
@Transactional
public class ApiController {

    @Autowired
    CategoryService categoryService;

    @SuppressWarnings("deprecation")
    @RequestMapping(value="/api/category/page", method=RequestMethod.GET)
    public Page<Category> getCategoryList() {
        return categoryService.findAll(new PageRequest(0, 10));
    }

}

I would like to retrieve the Page for use in the angularjs, which returns nothing: {} . While I getContent () the data is still available on request:

 [{"cid": 1, "cname": "Tien Hiep", "cmetaTitle": "tien-hiep", 
 "createDate": "May 6, 2018 1:04:58 PM "," createBy ":" Admin "," 
 modifiedDate ":" May 28, 2018 11:09:57 AM "," modifiedBy ":" Admin 
 "," cstatus " ..]

If I want to return the page type like:

    {
        "content":[
            {"cid": 1, "cname": "Tien Hiep", "cmetaTitle": "tien-hiep", "createDate": "May 6, 018 1:04:58 PM "," createBy ":" Admin "," modifiedDate ":" May 28, 2018 11:09:57 AM "," modifiedBy ":" Admin "," cstatus "}, 
            ...
        ],
        "last":false,
        "totalElements":10,
        "totalPages":4,
        "size":10,
        "number":0,
        "sort":null,
        "first":true,
    }

Can anybody help me fix that?

This is because you are returning List here, if you want response should be in format what you are looking for, then you should include DTO or bind that response in class and return that class.

Below I m giving small code snippet:

Class FoDto{
    List<Category> content;    
    // Getter and Setter method
} 

And in service layer you can prepare response.

If you are using maven

1.Add below jersey dependency to convert java object list to json

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jersey</artifactId>
</dependency>

or you can add Jax-rs dependency

<dependency>
    <groupId>javax.ws.rs</groupId>
    <artifactId>javax.ws.rs-api</artifactId>
    <version>2.0</version>
</dependency>

2) change method return type to List<Category> instead of Page<Category>

3) Add @Produces("application/json") annotation in above API. This is to convert the response to json format as you mentioned in your question.

Final outcome of your controller method.

@RequestMapping(value="/api/category/page", method=RequestMethod.GET)
@Produces("application/json")
public List<Category> getCategoryList() {
  Page<Category> pageCategory = categoryService.findAll(new PageRequest(0, 10));
  List<Category> catgoryList = pageCategory.getContent(); /*missed this conversion in my original post */
  return catgoryList;
}

If you have any problems or queries let me know.

Suppose your Dao is like bellow code:

import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.CrudRepository;
public interface CategoryDao extends CrudRepository<Category,Integer>{
List<Category> findAll(Pageable pageable);
}

Your Pagination Utility class is like this:

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
public class PaginationUtil {
    public static PageRequest makePageRequest(int pageId, int pageSize, Sort.Direction direction, String... properties){
        return pageSize> 0?new PageRequest(pageId, pageSize, direction, properties):null;
    }
}

Now you can get data with pagination with bellow way :

import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
@Autowired
private CategoryDao dao;
PageRequest pageRequest = PaginationUtil.makePageRequest(0, 10, Sort.Direction.DESC, "id");
List<Category> categories = dao
                .findAll(pageRequest);

Now categories list have your all data.

Your endpoint code will like this:

@RequestMapping(value="/api/category/page", method=RequestMethod.GET)
@Produces("application/json")
public List<Category> getCategoryList() {
    PageRequest pageRequest = PaginationUtil.makePageRequest(0, 10, Sort.Direction.DESC, "id");
    List<Category> categories = dao
                    .findAll(pageRequest);
    return categories;
}

More info visit this thread .

Thanks :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM