赞
踩
在使用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(); }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。