Return Spring 数据 JPA 中的页面<E> 为空
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 的页面,return 什么都没有:{}
。虽然我 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 " ..]
如果我想 return 页面类型如下:
{
"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,
}
谁能帮我解决这个问题?
这是因为您在此处 returning 列表,如果您希望响应的格式应符合您要查找的格式,那么您应该包含 DTO 或将该响应绑定到 class 和 return 即 class。
下面我给出了一小段代码:
Class FoDto{
List<Category> content;
// Getter and Setter method
}
并且在服务层你可以准备响应。
如果你使用的是maven
1.Add 下面的球衣依赖项将 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) 将方法 return 类型更改为
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);
}
你的分页工具class是这样的:
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;
}
更多信息请访问 this thread。
谢谢:)
@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 的页面,return 什么都没有:{}
。虽然我 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 " ..]
如果我想 return 页面类型如下:
{
"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,
}
谁能帮我解决这个问题?
这是因为您在此处 returning 列表,如果您希望响应的格式应符合您要查找的格式,那么您应该包含 DTO 或将该响应绑定到 class 和 return 即 class。
下面我给出了一小段代码:
Class FoDto{
List<Category> content;
// Getter and Setter method
}
并且在服务层你可以准备响应。
如果你使用的是maven
1.Add 下面的球衣依赖项将 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) 将方法 return 类型更改为
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);
}
你的分页工具class是这样的:
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;
}
更多信息请访问 this thread。
谢谢:)