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);
    }


}
相关推荐
AD钙奶-lalala20 小时前
Mac OS上搭建 http server
java
luckys.one20 小时前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
皮皮林5511 天前
SpringBoot 全局/局部双模式 Gzip 压缩实战:14MB GeoJSON 秒变 3MB
java·spring boot
小菜全1 天前
基于若依框架Vue+TS导出PDF文件的方法
javascript·vue.js·前端框架·json
weixin_456904271 天前
Spring Boot 用户管理系统
java·spring boot·后端
趁你还年轻_1 天前
异步编程CompletionService
java
DKPT1 天前
Java内存区域与内存溢出
java·开发语言·jvm·笔记·学习
sibylyue1 天前
Guava中常用的工具类
java·guava
奔跑吧邓邓子1 天前
【Java实战㉞】从0到1:Spring Boot Web开发与接口设计实战
java·spring boot·实战·web开发·接口设计
专注API从业者1 天前
Python/Java 代码示例:手把手教程调用 1688 API 获取商品详情实时数据
java·linux·数据库·python