繁体   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