当前位置: 首页 > 面试题库 >

具有分页API的Spring RestTemplate

壤驷睿
2023-03-14
问题内容

我们的REST API在Pages中返回结果。这是一个控制器的示例

@RequestMapping(value = "/search", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
@ResponseStatus(HttpStatus.OK)
public Page<MyObject> findAll(Pageable pageable) {
  ...
}

有没有一种简单的方法可以通过RestTemplate使用该API?

如果我们这样做

ParameterizedTypeReference<Page<MyObject>> responseType = new ParameterizedTypeReference<Page<MyObject>>() { };

ResponseEntity<Page<MyObject>> result = restTemplate.exchange(url, HttpMethod.GET, null/*httpEntity*/, responseType);

List<MyObject> searchResult = result.getBody().getContent();

引发异常

org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of org.springframework.data.domain.Page, 
problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information at [Source: java.io.PushbackInputStream@3be1e1f2; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of org.springframework.data.domain.Page, problem: abstract types either need to be mapped to concrete types, have custom deserializer, or be instantiated with additional type information

先感谢


问题答案:

将读取Rest API响应的代码更改为;

ParameterizedTypeReference<RestResponsePage<MyObject>> responseType = new ParameterizedTypeReference<RestResponsePage<MyObject>>() { };

ResponseEntity<RestResponsePage<MyObject>> result = restTemplate.exchange(url, HttpMethod.GET, null/*httpEntity*/, responseType);

List<MyObject> searchResult = result.getBody().getContent();

这是我为RestResponsePage创建的类

package com.basf.gb.cube.seq.vaadinui.util;

import java.util.ArrayList;
import java.util.List;

import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

public class RestResponsePage<T> extends PageImpl<T>{

  private static final long serialVersionUID = 3248189030448292002L;

  public RestResponsePage(List<T> content, Pageable pageable, long total) {
    super(content, pageable, total);
    // TODO Auto-generated constructor stub
  }

  public RestResponsePage(List<T> content) {
    super(content);
    // TODO Auto-generated constructor stub
  }

  /* PageImpl does not have an empty constructor and this was causing an issue for RestTemplate to cast the Rest API response
   * back to Page.
   */
  public RestResponsePage() {
    super(new ArrayList<T>());
  }

} 


 类似资料:
  • 我想使用和,有什么方法或可靠的解决办法可以使Pageable与jpaQuery一起工作吗?

  • 我在中实现了分页,因为还有行排序。当我点击列标题进行排序时,我的问题就出现了。它对10行的当前视图(行的视图大小)进行排序,而不是对整个数据进行排序。 我从分页中传递JTable的行值中获取分页代码。

  • 我们的分页采用的组件化实现方式,就是把一个分页菜单拆分成多个组件,比如 总页数 , 上一页, 等。 //定制分页组件 define( 'PAGE_TOTAL_NUM', 1<<0 ); //总页数 define( 'PAGE_PREV', 1<<1 ); //上一页 define( 'PAGE_DOT', 1<< 2); //省略号 define( 'PAGE_L

  • 主模块(App) 数据模块(具有网络和数据库依赖性的Android模块) 域模块(纯Kotlin模块) 为了引入分页,我不得不将类视为域类。(IMO并不是一个可怕的想法,因为最终是一个列表,并且数据源是抽象的) 因此,在域层中,我可以有一个类似的repository: Im aware具有和方法,但工厂驻留在数据层中。我的Viewmodel使用来自模型层的数据,在本例中,模型层是,据我所知,分页列

  • 由来 分页工具类并不是数据库分页的封装,而是分页方式的转换。在我们手动分页的时候,常常使用页码+每页个数的方式,但是有些数据库需要使用开始位置和结束位置来表示。很多时候这种转换容易出错(边界问题),于是封装了PageUtil工具类。 使用 transToStartEnd 将页数和每页条目数转换为开始位置和结束位置。 此方法用于不包括结束位置的分页方法。 例如: 页码:0,每页10 -> [0, 1

  • 我们的RESTAPI将返回页面中的结果。下面是一个控制器的示例 有没有一个简单的方法来消耗该API与重新设置模板? 如果我们这样做 它抛出一个异常 先谢谢你