1. <legend id='NGLOo'><style id='NGLOo'><dir id='NGLOo'><q id='NGLOo'></q></dir></style></legend>

    <tfoot id='NGLOo'></tfoot>

    <small id='NGLOo'></small><noframes id='NGLOo'>

  2. <i id='NGLOo'><tr id='NGLOo'><dt id='NGLOo'><q id='NGLOo'><span id='NGLOo'><b id='NGLOo'><form id='NGLOo'><ins id='NGLOo'></ins><ul id='NGLOo'></ul><sub id='NGLOo'></sub></form><legend id='NGLOo'></legend><bdo id='NGLOo'><pre id='NGLOo'><center id='NGLOo'></center></pre></bdo></b><th id='NGLOo'></th></span></q></dt></tr></i><div id='NGLOo'><tfoot id='NGLOo'></tfoot><dl id='NGLOo'><fieldset id='NGLOo'></fieldset></dl></div>
      <bdo id='NGLOo'></bdo><ul id='NGLOo'></ul>
    1. 如何使用页面&lt;Entity&gt;使用 Spring RestTemplate 响应

      时间:2024-08-24
    2. <small id='XR5gB'></small><noframes id='XR5gB'>

        <bdo id='XR5gB'></bdo><ul id='XR5gB'></ul>

        1. <legend id='XR5gB'><style id='XR5gB'><dir id='XR5gB'><q id='XR5gB'></q></dir></style></legend>
          • <i id='XR5gB'><tr id='XR5gB'><dt id='XR5gB'><q id='XR5gB'><span id='XR5gB'><b id='XR5gB'><form id='XR5gB'><ins id='XR5gB'></ins><ul id='XR5gB'></ul><sub id='XR5gB'></sub></form><legend id='XR5gB'></legend><bdo id='XR5gB'><pre id='XR5gB'><center id='XR5gB'></center></pre></bdo></b><th id='XR5gB'></th></span></q></dt></tr></i><div id='XR5gB'><tfoot id='XR5gB'></tfoot><dl id='XR5gB'><fieldset id='XR5gB'></fieldset></dl></div>

                <tbody id='XR5gB'></tbody>
              1. <tfoot id='XR5gB'></tfoot>

                本文介绍了如何使用页面&lt;Entity&gt;使用 Spring RestTemplate 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在使用 spring 数据 (mongoDb),并且我有我的存储库:

                I'm using spring data (mongoDb) and I've got my repository:

                public interface StoriesRepository extends PagingAndSortingRepository<Story, String> {}
                

                然后我有一个控制器:

                @RequestMapping(method = RequestMethod.GET)
                public ResponseEntity<Page<StoryResponse>> getStories(Pageable pageable) {
                    Page<StoryResponse> stories = storiesRepository.findAll(pageable).map(StoryResponseMapper::toStoryResponse);
                    return ResponseEntity.ok(stories);
                }
                

                一切正常,但我无法使用 RestTemplate getForEntity 方法使用我的端点:

                Everything works fine, but I can't consume my endpoint using RestTemplate getForEntity method:

                def entity = restTemplate.getForEntity(getLocalhost("/story"), new TypeReference<Page<StoryResponse>>(){}.class)
                

                我应该提供什么类来成功反序列化我的实体页面?

                What class should I provide to successfully deserialize my Page of entities?

                推荐答案

                new TypeReference<Page<StoryResponse>>() {}
                

                此语句的问题在于 Jackson 无法实例化抽象类型.您应该向 Jackson 提供有关如何使用具体类型实例化 Page 的信息.但它的具体类型 PageImpl 没有默认构造函数或任何 @JsonCreator,因此您也可以使用以下代码:

                The problem with this statement is that Jackson cannot instantiate an abstract type. You should give Jackson the information on how to instantiate Page with a concrete type. But its concrete type, PageImpl, has no default constructor or any @JsonCreators for that matter, so you can not use the following code either:

                new TypeReference<PageImpl<StoryResponse>>() {}
                

                由于您无法将所需的信息添加到 Page 类中,因此最好为 Page 接口创建一个自定义实现,该接口具有默认的无参数构造函数,就像在这个 answer 中一样.然后在类型引用中使用该自定义实现,如下所示:

                Since you can't add the required information to the Page class, It's better to create a custom implementation for Page interface which has a default no-arg constructor, as in this answer. Then use that custom implementation in type reference, like following:

                new TypeReference<CustomPageImpl<StoryResponse>>() {}
                

                这是从链接问题复制的自定义实现:

                Here are the custom implementation, copied from linked question:

                public class CustomPageImpl<T> extends PageImpl<T> {
                    private static final long serialVersionUID = 1L;
                    private int number;
                    private int size;
                    private int totalPages;
                    private int numberOfElements;
                    private long totalElements;
                    private boolean previousPage;
                    private boolean firstPage;
                    private boolean nextPage;
                    private boolean lastPage;
                    private List<T> content;
                    private Sort sort;
                
                    public CustomPageImpl() {
                        super(new ArrayList<>());
                    }
                
                    @Override
                    public int getNumber() {
                        return number;
                    }
                
                    public void setNumber(int number) {
                        this.number = number;
                    }
                
                    @Override
                    public int getSize() {
                        return size;
                    }
                
                    public void setSize(int size) {
                        this.size = size;
                    }
                
                    @Override
                    public int getTotalPages() {
                        return totalPages;
                    }
                
                    public void setTotalPages(int totalPages) {
                        this.totalPages = totalPages;
                    }
                
                    @Override
                    public int getNumberOfElements() {
                        return numberOfElements;
                    }
                
                    public void setNumberOfElements(int numberOfElements) {
                        this.numberOfElements = numberOfElements;
                    }
                
                    @Override
                    public long getTotalElements() {
                        return totalElements;
                    }
                
                    public void setTotalElements(long totalElements) {
                        this.totalElements = totalElements;
                    }
                
                    public boolean isPreviousPage() {
                        return previousPage;
                    }
                
                    public void setPreviousPage(boolean previousPage) {
                        this.previousPage = previousPage;
                    }
                
                    public boolean isFirstPage() {
                        return firstPage;
                    }
                
                    public void setFirstPage(boolean firstPage) {
                        this.firstPage = firstPage;
                    }
                
                    public boolean isNextPage() {
                        return nextPage;
                    }
                
                    public void setNextPage(boolean nextPage) {
                        this.nextPage = nextPage;
                    }
                
                    public boolean isLastPage() {
                        return lastPage;
                    }
                
                    public void setLastPage(boolean lastPage) {
                        this.lastPage = lastPage;
                    }
                
                    @Override
                    public List<T> getContent() {
                        return content;
                    }
                
                    public void setContent(List<T> content) {
                        this.content = content;
                    }
                
                    @Override
                    public Sort getSort() {
                        return sort;
                    }
                
                    public void setSort(Sort sort) {
                        this.sort = sort;
                    }
                
                    public Page<T> pageImpl() {
                        return new PageImpl<>(getContent(), new PageRequest(getNumber(),
                                getSize(), getSort()), getTotalElements());
                    }
                }
                

                这篇关于如何使用页面&lt;Entity&gt;使用 Spring RestTemplate 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Spring CrudRepository findByInventoryIds(List&lt;Long&am 下一篇:如何将 @Transactional 与 Spring Data 一起使用?

                相关文章

                  <bdo id='utwQd'></bdo><ul id='utwQd'></ul>
              2. <legend id='utwQd'><style id='utwQd'><dir id='utwQd'><q id='utwQd'></q></dir></style></legend>
                1. <tfoot id='utwQd'></tfoot>

                2. <i id='utwQd'><tr id='utwQd'><dt id='utwQd'><q id='utwQd'><span id='utwQd'><b id='utwQd'><form id='utwQd'><ins id='utwQd'></ins><ul id='utwQd'></ul><sub id='utwQd'></sub></form><legend id='utwQd'></legend><bdo id='utwQd'><pre id='utwQd'><center id='utwQd'></center></pre></bdo></b><th id='utwQd'></th></span></q></dt></tr></i><div id='utwQd'><tfoot id='utwQd'></tfoot><dl id='utwQd'><fieldset id='utwQd'></fieldset></dl></div>
                3. <small id='utwQd'></small><noframes id='utwQd'>