簡體   English   中英

返回頁面<E>在 Spring 數據 JPA 中為空

[英]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));
    }

}

我想檢索要在 angularjs 中使用的頁面,它不返回任何內容: {} 當我getContent () ,數據仍可應要求提供:

 [{"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 " ..]

如果我想返回頁面類型,如:

    {
        "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,
    }

有人可以幫我解決這個問題嗎?

這是因為您在此處返回 List,如果您希望響應的格式應為您要查找的格式,那么您應該包含 DTO 或在類中綁定該響應並返回該類。

下面我給出了小代碼片段:

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

在服務層,您可以准備響應。

如果您使用的是 Maven

1.添加下面的球衣依賴,將java對象列表轉換為json

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

或者您可以添加 Jax-rs 依賴項

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

2) 將方法返回類型更改為List<Category>而不是Page<Category>

3) 在上述 API 中添加@Produces("application/json")注解。 這是將響應轉換為您在問題中提到的 json 格式。

控制器方法的最終結果。

@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;
}

如果您有任何問題或疑問,請告訴我。

假設你的 Dao 就像下面的代碼:

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

你的 Pagination Utility 類是這樣的:

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;
    }
}

現在您可以通過以下方式獲取分頁數據:

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);

現在類別列表包含您的所有數據。

您的端點代碼將如下所示:

@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;
}

更多信息請訪問此線程

謝謝 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM