Spring MVC 3:将Spring-Data页面作为JSON返回

我有一个用Spring-Data制作的数据访问层。 我现在正在创建一个Web应用程序。 这个控制器方法应该返回格式化为JSON的Spring-Data页面 。

这样的页面是一个列表,其中包含额外的分页信息,例如记录总量等等。

这是可能的,如果是的话怎么样?

与此直接相关,我可以定义属性名称的映射吗? 例如。 意思是我需要定义如何在JSON中命名分页信息属性(与页面不同)。 这可能吗?怎么样?

Spring HATEOAS和Spring Data Commons即将推出这样的场景。 Spring HATEOAS附带一个PageMetadata对象,它基本上包含与Page相同的数据,但执行方式较少,因此可以更容易地编组和解组。

我们与Spring HATEOAS和Spring Data公共结合实现这一点的另一个方面是,简单地封送页面,内容和元数据,但也想生成可能存在的下一页或上一页的链接,这样的价值很小,所以客户端不必构造URI来遍历这些页面本身。

一个例子

假设一个域类Person

 class Person { Long id; String firstname, lastname; } 

以及它的相应存储库:

 interface PersonRepository extends PagingAndSortingRepository { } 

您现在可以按如下方式公开Spring MVC控制器:

 @Controller class PersonController { @Autowired PersonRepository repository; @RequestMapping(value = "/persons", method = RequestMethod.GET) HttpEntity> persons(Pageable pageable, PagedResourcesAssembler assembler) { Page persons = repository.findAll(pageable); return new ResponseEntity<>(assembler.toResources(persons), HttpStatus.OK); } } 

这里可能有相当多的解释。 让我们一步一步来:

  1. 我们有一个Spring MVC控制器将存储库连接到它。 这需要设置Spring Data(通过@Enable(Jpa|Mongo|Neo4j|Gemfire)Repositories或XML等价物)。 控制器方法映射到/persons ,这意味着它将接受对该方法的所有GET请求。
  2. 从该方法返回的核心类型是PagedResources – 一个来自Spring HATEOAS的类型,它表示一些富含LinksPageMetadata
  3. 调用该方法时,Spring MVC必须为PageablePagedResourcesAssembler创建实例。 为了实现这一目标,您需要通过即将在Spring Data Commons的里程碑中引入的@EnableSpringDataWebSupport注释或通过独立的bean定义( 此处记录 )来启用Spring Data Web支持。

    Pageable将填充来自请求的信息。 默认配置会将?page=0&size=10转换为Pageable请求页面大小为10的第一页。

    PageableResourcesAssembler允许您轻松将Page转换为PagedResources实例。 它不仅会将页面元数据添加到响应中,还会根据您访问的页面以及如何配置Pageable分辨率,将相应的链接添加到表示中。

为JPA启用此示例的示例JavaConfig配置如下所示:

 @Configuration @EnableWebMvc @EnableSpringDataWebSupport @EnableJpaRepositories class ApplicationConfig { // declare infrastructure components like EntityManagerFactory etc. here } 

样本请求和响应

假设我们在数据库中有30 Persons 。 您现在可以触发请求GET http://localhost:8080/persons ,您将看到与此类似的内容:

 { "links" : [ { "rel" : "next", "href" : "http://localhost:8080/persons?page=1&size=20 } ], "content" : [ … // 20 Person instances rendered here ], "pageMetadata" : { "size" : 20, "totalElements" : 30, "totalPages" : 2, "number" : 0 } } 

请注意,汇编程序生成了正确的URI,并且还选择了当前的默认配置,以将参数解析为即将发出的请求的Pageable 。 这意味着,如果您更改该配置,链接将自动遵循更改。 默认情况下,汇编程序指向它所调用的控制器方法,但可以通过PagedResourcesAssembler.toResource(…)自定义Link来自定义,该Link用作构建PagedResourcesAssembler.toResource(…)方法重载的分页链接。

外表

PagedResourcesAssembler位将在即将发布的Spring Data Babbage 版本列的里程碑版本中提供。 它已在当前快照中可用。 您可以在我的Spring RESTBucks 示例应用程序中看到这个的一个工作示例。 只需克隆它,运行mvn jetty:run并curlhttp://localhost:8080/pages

奥利弗,你的答案很棒,我把它标记为答案。 这里只是为了完整性我想出的平均时间可能对其他人有用。

我使用JQuery Datatables作为我的网格/表格小部件。 它向服务器发送非常具体的参数,并且除了非常具体的响应之外:请参阅http://datatables.net/usage/server-side 。

为了实现这一点,创建了一个反映数据表所需内容的自定义辅助对象。 请注意,getter和setter必须像它们一样命名,否则生成的json是错误的(区分大小写的属性名称和数据表使用此“伪匈牙利表示法”…)。

 public class JQueryDatatablesPage implements java.io.Serializable { private final int iTotalRecords; private final int iTotalDisplayRecords; private final String sEcho; private final List aaData; public JQueryDatatablesPage(final List pageContent, final int iTotalRecords, final int iTotalDisplayRecords, final String sEcho){ this.aaData = pageContent; this.iTotalRecords = iTotalRecords; this.iTotalDisplayRecords = iTotalDisplayRecords; this.sEcho = sEcho; } public int getiTotalRecords(){ return this.iTotalRecords; } public int getiTotalDisplayRecords(){ return this.iTotalDisplayRecords; } public String getsEcho(){ return this.sEcho; } public List getaaData(){ return this.aaData; } } 

第二部分是相应控制器中的方法:

 @RequestMapping(value = "/search", method = RequestMethod.GET, produces = "application/json") public @ResponseBody String search ( @RequestParam int iDisplayStart, @RequestParam int iDisplayLength, @RequestParam int sEcho, // for datatables draw count @RequestParam String search) throws IOException { int pageNumber = (iDisplayStart + 1) / iDisplayLength; PageRequest pageable = new PageRequest(pageNumber, iDisplayLength); Page page = compoundService.myCustomSearchMethod(search, pageable); int iTotalRecords = (int) (int) page.getTotalElements(); int iTotalDisplayRecords = page.getTotalPages() * iDisplayLength; JQueryDatatablesPage dtPage = new JQueryDatatablesPage<>( page.getContent(), iTotalRecords, iTotalDisplayRecords, Integer.toString(sEcho)); String result = toJson(dtPage); return result; } private String toJson(JQueryDatatablesPage dt) throws IOException { ObjectMapper mapper = new ObjectMapper(); mapper.registerModule(new Hibernate4Module()); return mapper.writeValueAsString(dt); } 

compoundService由Spring-Data存储库提供支持。 它管理事务和方法级安全性。 toJSON()方法使用Jackson 2.0,你需要将相应的模块注册到mapper,在我的情况下是hibernate 4。

如果您有双向关系,则需要使用注释所有实体类

 @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="jsonId") 

这使Jackson 2.0能够序列化循环依赖(在早期版本中是不可能的,并且需要对您的实体进行注释)。

您需要添加以下依赖项:

  com.fasterxml.jackson.core jackson-core 2.2.1   com.fasterxml.jackson.datatype jackson-datatype-hibernate4 2.2.1   com.fasterxml.jackson.core jackson-annotations 2.2.1 jar  

使用Spring Boot(以及Mongo DB)我能够成功完成以下任务:

 @RestController @RequestMapping("/product") public class ProductController { //... @RequestMapping(value = "/all", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE }) HttpEntity> get(@PageableDefault Pageable p, PagedResourcesAssembler assembler) { Page product = productRepository.findAll(p); return new ResponseEntity<>(assembler.toResource(product), HttpStatus.OK); } } 

而模型类是这样的:

 @Document(collection = "my_product") @Data @ToString(callSuper = true) public class Product extends BaseProduct { private String itemCode; private String brand; private String sku; }