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


}
相关推荐
SL_staff10 小时前
《如何用规则引擎替代if-else?JVS-Rules可视化编排比硬编码强在哪里?》
java·低代码·架构
Sam_Deep_Thinking10 小时前
java中的class到底是个什么东西?
java·开发语言·面试
swordbob10 小时前
Spring 3 级缓存解决循环依赖
java·spring
摇滚侠10 小时前
SpringMVC 入门到实战 获取请求参数 25-32
java·spring·intellij-idea
咖啡八杯10 小时前
【无标题】
java·后端·设计模式
mqiqe10 小时前
面试题-MyBatis 面试篇
java·面试·mybatis
摇滚侠10 小时前
SpringMVC 入门到实战 @RequestMapping 14-24
java·spring
云烟成雨TD10 小时前
Spring AI Alibaba 1.x 系列【80】可观测集成
java·人工智能·spring
Filwaod10 小时前
MCP 接入模式对比:Agent - Gateway - 业务项目 vs Agent - Adapter - 业务项目
java·agent·mcp
kuonyuma10 小时前
MyBatis入门·注解操作
java·spring boot·mysql·spring·mybatis