当前位置:   article > 正文

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

feign date json parse error

在使用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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
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;
            }
        };
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
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;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44

解决方法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();
}
  • 1
  • 2
  • 3
  • 4
  • 5
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
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/896770
推荐阅读
相关标签
  

闽ICP备14008679号