【问题标题】:Spring MVC 3: return a Spring-Data Page as JSONSpring MVC 3:以 JSON 形式返回 Spring-Data 页面
【发布时间】:2013-05-23 07:48:20
【问题描述】:

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

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

这可能吗?如果可以,怎么做?

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

【问题讨论】:

    标签: java json spring-mvc spring-data spring-hateoas


    【解决方案1】:

    在 Spring HATEOAS 和 Spring Data Commons 中支持这样的场景。 Spring HATEOAS 附带了一个 PageMetadata 对象,该对象本质上包含与 Page 相同的数据,但执行方式较少,因此可以更轻松地编组和解组。

    我们结合 Spring HATEOAS 和 Spring Data commons 实现此功能的另一个原因是,简单地编组页面、内容和元数据没有什么价值,但还希望生成可能存在的下一页或上一页的链接,因此客户端不必自己构造 URI 来遍历这些页面。

    一个例子

    假设一个域类Person

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

    以及它对应的仓库:

    interface PersonRepository extends PagingAndSortingRepository<Person, Long> { }
    

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

    @Controller
    class PersonController {
    
      @Autowired PersonRepository repository;
    
      @RequestMapping(value = "/persons", method = RequestMethod.GET)
      HttpEntity<PagedResources<Person>> persons(Pageable pageable, 
        PagedResourcesAssembler assembler) {
    
        Page<Person> 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 定义(记录在 here)来启用 Spring Data Web 支持。

      Pageable 将填充来自请求的信息。默认配置会将?page=0&amp;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 以用于即将到来的请求。这意味着,如果您更改该配置,链接将自动遵循更改。默认情况下,汇编器指向调用它的控制器方法,但可以通过提交自定义Link 来自定义该方法,以用作构建指向PagedResourcesAssembler.toResource(…) 方法重载的分页链接的基础。

    展望

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

    【讨论】:

    • 哇。听起来很棒。一个问题:是否可以自定义其工作方式?不同的“JavaScript 网格”向服务器发送了不同的分页参数,并且需要在响应中设置某些属性。在 m 情况下,我主要使用datatables.net
    • 是的,只需自定义 PageableHandlerMethodArgumentResolver Spring bean (Javadoc here)。这将导致 Pageable 解析为配置以及相应呈现的分页链接。
    • @OliverGierke with configuration defined at XML 我收到以下错误:无法将 [org.springframework.data.web.config.HateoasAwareSpringDataWebConfiguration] 类型的值转换为所需类型 [org.springframework.web.method .support.HandlerMethodArgumentResolver] 用于属性“customArgumentResolvers[0]”:未找到匹配的编辑器或转换策略。使用 Java 配置,everythink 工作正常。
    • @OliverGierke:如果应用程序是 RESTful 的呢?我的意思是我们使用同一个控制器来处理像person.jspperson.json这样的请求?
    • Spring 存储库返回诸如“_links”和“_embedded”之类的属性。如果我们能生成相同的东西,那就太好了,这样我们的客户端代码就可以正常工作了。
    【解决方案2】:

    奥利弗,你的回答很棒,我将其标记为答案。这里只是为了完整起见,我想出的同时可能对其他人有用。

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

    为了实现这一点,创建了一个反映数据表期望的自定义​​帮助对象。请注意,getter 和 setter 必须像它们一样命名,否则生成的 json 是错误的(区分大小写的属性名称和数据表使用这种“伪匈牙利符号”...)。

    public class JQueryDatatablesPage<T> implements java.io.Serializable {
    
        private final int iTotalRecords;
        private final int iTotalDisplayRecords;
        private final String sEcho;
        private final List<T> aaData;
    
        public JQueryDatatablesPage(final List<T> 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<T> 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<SimpleCompound> page = compoundService.myCustomSearchMethod(search, pageable);
        int iTotalRecords = (int) (int) page.getTotalElements();
        int iTotalDisplayRecords = page.getTotalPages() * iDisplayLength;
        JQueryDatatablesPage<SimpleCompound> 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,您需要将适当的模块注册到映射器,在我的情况下为 hibernate 4。

    如果您有双向关系,则需要使用

    注释所有实体类
    @JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="jsonId")
    

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

    您需要添加以下依赖项:

    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.datatype</groupId>
        <artifactId>jackson-datatype-hibernate4</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-annotations</artifactId>
        <version>2.2.1</version>
        <type>jar</type>
    </dependency>
    

    【讨论】:

      【解决方案3】:

      使用 Spring Boot(和 Mongo DB)我能够成功地完成以下操作:

      @RestController
      @RequestMapping("/product")
      public class ProductController {
         //...
          @RequestMapping(value = "/all", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_VALUE })
             HttpEntity<PagedResources<Product>> get(@PageableDefault Pageable p, PagedResourcesAssembler assembler) {
             Page<Product> 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;    
      }
      

      【讨论】:

        猜你喜欢
        • 2013-08-08
        • 1970-01-01
        • 2013-06-11
        • 2018-01-11
        • 2012-09-26
        • 1970-01-01
        • 2015-07-25
        • 1970-01-01
        • 2014-09-26
        相关资源
        最近更新 更多