openFeign 调用后 返回 出现 application/json 错误

项目场景:

远程调用时返回json格式错误

项目场景:从分页插件式改换为原生分页的时候 通过openFeign调用时发现了问题


问题描述

不需要openFeign 调用的时候 返回的数据和格式是对 通过openFeign 调用后返回 出现 application/json 错误 :

java 复制代码
org.springframework.web.client.RestClientException: Error while extracting response for type [cn.cws.framework.core.datasource.util.CwsPage<cn.cws.fulimall.vo.bill.backstage.OrderEntryListVo>] and content type [application/json]

原因分析:

远程调用后 转换为json格式传输时 错误:

  1. JSON 结构与 Java 对象不匹配
  2. RestTemplate 配置问题
  3. 使用正确的类型引用进行 REST 调用
  4. JSON 字段校验
    通过以上的步骤 发现问题出现在这个类 CwsPage 少了一个 setList() 方法
java 复制代码
package cn.cws.framework.core.datasource.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import lombok.ToString;

import java.util.List;
import java.util.function.Function;

import static java.util.stream.Collectors.toList;


@ToString
@ApiModel(value = "CwsPage",description = "分页返回数据含分页相关数据")
public class CwsPage<T> extends Page<T> {

    public CwsPage() {
        super();
    }

    public CwsPage(long current, long size) {
        super(current, size);
    }

    public CwsPage(long current, long size, long total) {
        super(current, size, total);
    }

    public CwsPage(long current, long size, boolean searchCount) {
        super(current, size, searchCount);
    }

    public CwsPage(long current, long size, long total, boolean searchCount) {
        super(current, size, total, searchCount);
    }

    @Deprecated
    @JsonIgnore
    public long getCurrent() {
        return this.current;
    }

    @Deprecated
    @JsonIgnore
    public long getSize() {
        return this.size;
    }

    @Deprecated
    @JsonIgnore
    public List<T> getRecords() {
        return this.records;
    }
    
    

    /**
     * 兼容前端旧数据,现在使用getCurrent
     * @return
     */

    public Long getPageNum() {
        return super.getCurrent();
    }

    /**
     * 兼容前端旧数据,现在使用getSize
     * @return
     */
    public Long getPageSize() {
        return super.getSize();
    }

    /**
     * 兼容前端旧数据,现在使用getRecords
     * @return
     */
    public List<T> getList() {
        return super.getRecords();
    }
    
    



    public Boolean getLastPage() {
        return super.getPages() == super.getCurrent();
    }

    public Boolean getHasNextPage() {
        return super.getPages() > super.getCurrent();
    }

    public CwsPage<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    public <R> CwsPage<R> convert(Function<? super T, ? extends R> mapper) {
        List<R> collect = this.getList().stream().map(mapper).collect(toList());
        return ((CwsPage<R>) this).setRecords(collect);
    }


}

解决方案:

通过上面原因分析4步骤 把json 进行解析 发现出问题:

java 复制代码
package cn.cws.framework.core.datasource.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonIgnore;
import io.swagger.annotations.ApiModel;
import lombok.ToString;

import java.util.List;
import java.util.function.Function;

import static java.util.stream.Collectors.toList;


@ToString
@ApiModel(value = "CwsPage",description = "分页返回数据含分页相关数据")
public class CwsPage<T> extends Page<T> {

    public CwsPage() {
        super();
    }

    public CwsPage(long current, long size) {
        super(current, size);
    }

    public CwsPage(long current, long size, long total) {
        super(current, size, total);
    }

    public CwsPage(long current, long size, boolean searchCount) {
        super(current, size, searchCount);
    }

    public CwsPage(long current, long size, long total, boolean searchCount) {
        super(current, size, total, searchCount);
    }

    @Deprecated
    @JsonIgnore
    public long getCurrent() {
        return this.current;
    }

    @Deprecated
    @JsonIgnore
    public long getSize() {
        return this.size;
    }

    @Deprecated
    @JsonIgnore
    public List<T> getRecords() {
        return this.records;
    }

    public void setList(List<T> list){
        this.records = list;
    }

    /**
     * 兼容前端旧数据,现在使用getCurrent
     * @return
     */

    public Long getPageNum() {
        return super.getCurrent();
    }

    /**
     * 兼容前端旧数据,现在使用getSize
     * @return
     */
    public Long getPageSize() {
        return super.getSize();
    }

    /**
     * 兼容前端旧数据,现在使用getRecords
     * @return
     */
    public List<T> getList() {
        return super.getRecords();
    }






    public Boolean getLastPage() {
        return super.getPages() == super.getCurrent();
    }

    public Boolean getHasNextPage() {
        return super.getPages() > super.getCurrent();
    }

    public CwsPage<T> setRecords(List<T> records) {
        this.records = records;
        return this;
    }

    public <R> CwsPage<R> convert(Function<? super T, ? extends R> mapper) {
        List<R> collect = this.getList().stream().map(mapper).collect(toList());
        return ((CwsPage<R>) this).setRecords(collect);
    }


}
相关推荐
RainbowSea11 小时前
12. LangChain4j + 向量数据库操作详细说明
java·langchain·ai编程
RainbowSea11 小时前
11. LangChain4j + Tools(Function Calling)的使用详细说明
java·langchain·ai编程
考虑考虑15 小时前
Jpa使用union all
java·spring boot·后端
用户37215742613516 小时前
Java 实现 Excel 与 TXT 文本高效互转
java
浮游本尊17 小时前
Java学习第22天 - 云原生与容器化
java
渣哥18 小时前
原来 Java 里线程安全集合有这么多种
java
间彧19 小时前
Spring Boot集成Spring Security完整指南
java
间彧19 小时前
Spring Secutiy基本原理及工作流程
java
Java水解20 小时前
JAVA经典面试题附答案(持续更新版)
java·后端·面试
洛小豆1 天前
在Java中,Integer.parseInt和Integer.valueOf有什么区别
java·后端·面试