简体   繁体   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));
    }

}

I would like to retrieve the Page for use in the angularjs, which returns nothing: {} .我想检索要在 angularjs 中使用的页面,它不返回任何内容: {} While I getContent () the data is still available on request:当我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 " ..]

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.这是因为您在此处返回 List,如果您希望响应的格式应为您要查找的格式,那么您应该包含 DTO 或在类中绑定该响应并返回该类。

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如果您使用的是 Maven

1.Add below jersey dependency to convert java object list to json 1.添加下面的球衣依赖,将java对象列表转换为json

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

or you can add Jax-rs dependency或者您可以添加 Jax-rs 依赖项

<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> 2) 将方法返回类型更改为List<Category>而不是Page<Category>

3) Add @Produces("application/json") annotation in above API. 3) 在上述 API 中添加@Produces("application/json")注解。 This is to convert the response to json format as you mentioned in your question.这是将响应转换为您在问题中提到的 json 格式。

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:假设你的 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);
}

Your Pagination Utility class is like this:你的 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;
    }
}

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 :)谢谢 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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