侧边栏壁纸
  • 累计撰写 14 篇文章
  • 累计创建 22 个标签
  • 累计收到 0 条评论

目 录CONTENT

文章目录

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

Administrator
2024-05-15 / 0 评论 / 0 点赞 / 37 阅读 / 15293 字

项目场景:

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

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


问题描述

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

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() 方法
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 进行解析 发现出问题:

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


}

解决后 测试结果如下
1715752319741.png

0
广告 广告

评论区