Feign 调用对象时:JSON parse error:Cannot deserialize value of type `java.util.Date` from String

在使用Feign调用其他服务接口时遇到Date类型无法反序列化的错误,可以通过两种方式解决:1) 在实体类上添加@JsonFormat注解并指定日期格式;2) 将返回类型改为Object,通过JSONObject解析成实体类。示例代码展示了这两种方法的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用feign调用其他服务接口时,如果对象存在Date类型就会报错Cannot deserialize value of type java.util.Date from String

解决方法1:在调用端的model类上加上注解,必须有无参构造器
@JsonFormat(pattern = “yyyy-MM-dd”, timezone = “GMT+8”)

@FeignClient(value = "${feignTestArms}",fallbackFactory = FeignClientTestFactory.class)
public interface FeignTest {
    @RequestMapping(value = "/pow/arms/v1/integration/organization/ba",method = RequestMethod.GET)
    List<TestModel> getAllOrganizations();
}
package com.gsafety.pow.integration.feign.client.hystrix;

import com.gsafety.pow.integration.feign.client.FeignTest;
import com.gsafety.pow.integration.model.resource.OrganizationModel;
import com.gsafety.pow.integration.model.resource.TestModel;
import feign.hystrix.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.List;
@Component
public class FeignClientTestFactory implements FallbackFactory<FeignTest> {
    @Override
    public FeignTest create(Throwable cause) {
        return new FeignTest() {
            @Override
            public List<TestModel> getAllOrganizations() {
                System.out.println("kang");
                cause.printStackTrace();
                return null;
            }
        };
    }
}
package com.gsafety.pow.integration.model.resource;

import com.fasterxml.jackson.annotation.JsonFormat;

import java.util.Date;

public class TestModel {
    private String name;
    private String value;
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss" )
    private Date startTime;
    public TestModel(){

    }
    public TestModel(String name, String value,Date startTime) {
        this.name = name;
        this.value = value;
        this.startTime = startTime;
    }

    public String getName() {
        return name;
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

解决方法2:调用时,返回object类型,然后通过JSONObject 解析成实体类

@FeignClient(value = "${feignTestArms}",fallbackFactory = FeignClientTestFactory.class)
public interface FeignTest {
    @RequestMapping(value = "/pow/arms/v1/integration/organization/ba",method = RequestMethod.GET)
    Object getAllOrganizations();
}
package com.gsafety.pow.integration.feign.client;

import com.alibaba.fastjson.JSONObject;
import com.gsafety.pow.integration.model.resource.OrganizationModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController(value = "kang/")
@RequestMapping("/kang")
public class FeignTestController {
    @Autowired
    private FeignTest feignTest;

    @GetMapping(value = "/feignTest")
    public List getOrgans() {
        Object allOrganizations = feignTest.getAllOrganizations();
        JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(allOrganizations));
        System.out.println("kang" + jsonObject);
        String data = jsonObject.getString("data");
        List list = JSONObject.parseObject(data, List.class);
        return list;
//        return feignTest.getAllOrganizations();
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值