openfeign get请求参数dto出现错误怎么解决
发表于:2025-02-04 作者:千家信息网编辑
千家信息网最后更新 2025年02月04日,本篇内容主要讲解"openfeign get请求参数dto出现错误怎么解决",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"openfeign get请求参数
千家信息网最后更新 2025年02月04日openfeign get请求参数dto出现错误怎么解决
本篇内容主要讲解"openfeign get请求参数dto出现错误怎么解决",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"openfeign get请求参数dto出现错误怎么解决"吧!
项目中使用的是spring boot 2.3.3,spring-cloud Hoxton.SR8.
在使用feign调用服务时 使用@GetMapping 和 @SpringQueryMap 和传输DTO对象,其中DTO对象中包含LocalDateTime属性,一直报类型转换异常,无法调用服务。解决方法有很多,找了网上很多解决办法都没效果,大体都是FastJson 序列化之类的(可能每个项目差异吧), 解决过程分析暂不分析吧。先行记录一下,因为看到网上很多人貌似都遇到过这个问题。以下是服务提供方
@FeignClient(value = "user-service", path = "/user/v1")public interface UserClient {@GetMapping("/")PageVOgetUserList(@SpringQueryMap UserDTO userDTO);}
@Data@ApiModel(value = "运营平台用户列表查询参数")public class UserDTO implements Serializable { private static final long serialVersionUID = -3767202379100110105L; @ApiModelProperty(value = "用户id") private Long id; @Size(max = 12, message = "nickName:用户昵称最大长度为12") @ApiModelProperty(value = "用户昵称") private String nickName; @ApiModelProperty(value = "手机号码") private String phone; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @ApiModelProperty(value = "创建时间开始") private LocalDateTime createdAtStart; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @ApiModelProperty(value = "创建时间结束") private LocalDateTime createdAtEnd; @ApiModelProperty(value = "页码", required = true) private Integer page; @ApiModelProperty(value = "每页条数", required = true) private Integer size;}
核心重点:新增一个QueryMapEncoder
import feign.Param;import feign.QueryMapEncoder;import feign.codec.EncodeException;import java.beans.IntrospectionException;import java.beans.Introspector;import java.beans.PropertyDescriptor;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import java.time.LocalDateTime;import java.time.format.DateTimeFormatter;import java.util.*;public class LocalDateTimeQueryMapEncoder implements QueryMapEncoder { private final Map, ObjectParamMetadata> classToMetadata = new HashMap , ObjectParamMetadata>(); private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); @Override public Map encode(Object object) throws EncodeException { try { ObjectParamMetadata metadata = getMetadata(object.getClass()); Map propertyNameToValue = new HashMap (); for (PropertyDescriptor pd : metadata.objectProperties) { Method method = pd.getReadMethod(); Object value = method.invoke(object); if (value != null && value != object) { Param alias = method.getAnnotation(Param.class); String name = alias != null ? alias.value() : pd.getName(); if("java.time.LocalDateTime".equals(method.getReturnType().getName())){ //propertyNameToValue.put(name, "2020-10-07 01:01:00"); propertyNameToValue.put(name, dtf.format((LocalDateTime)value)); }else{ propertyNameToValue.put(name, value); } } } return propertyNameToValue; } catch (IllegalAccessException | IntrospectionException | InvocationTargetException e) { throw new EncodeException("Failure encoding object into query map", e); } } private ObjectParamMetadata getMetadata(Class> objectType) throws IntrospectionException { ObjectParamMetadata metadata = classToMetadata.get(objectType); if (metadata == null) { metadata = ObjectParamMetadata.parseObjectType(objectType); classToMetadata.put(objectType, metadata); } return metadata; } private static class ObjectParamMetadata { private final List objectProperties; private ObjectParamMetadata(List objectProperties) { this.objectProperties = Collections.unmodifiableList(objectProperties); } private static ObjectParamMetadata parseObjectType(Class> type) throws IntrospectionException { List properties = new ArrayList (); for (PropertyDescriptor pd : Introspector.getBeanInfo(type).getPropertyDescriptors()) { boolean isGetterMethod = pd.getReadMethod() != null && !"class".equals(pd.getName()); if (isGetterMethod) { properties.add(pd); } } return new ObjectParamMetadata(properties); } }}
@Configurationpublic class CustomFeignConfig { @Bean public Feign.Builder feignBuilder() { return Feign.builder() .queryMapEncoder(new LocalDateTimeQueryMapEncoder()) .retryer(Retryer.NEVER_RETRY); }}
到此,相信大家对"openfeign get请求参数dto出现错误怎么解决"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
参数
用户
错误
服务
内容
对象
方法
时间
昵称
项目
分析
学习
查询
实用
更深
最大
供方
兴趣
办法
号码
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
淘宝软件开发没按合同时间做完
巨彬数据库
数据库原理与应用分析设计题
安徽软件开发哪家实惠
中邦网络安全技术(深圳)有限公司招聘
上海ibm软件开发
数据库系统教程考点
食堂就餐管理系统数据库
英雄联盟手游那个服务器有中文
云服务器租用提供商
上海宁丞互联网科技公司
网络安全问题及解决策略
绵阳京东方软件开发招聘
通信软件开发行业前景
计算机网络技术湖南高职排名
id产品车联网免费服务器
天津起源网络技术有限公司邮编
网络安全管 保密 管理
如何进入mc2b2t服务器
守护网络安全构建和谐校园作文
河南卓晨网络技术
数据库找用户名密码
t3连接系统数据库失败
怎么看待网络安全使用
做一名软件开发师作文
疫情中网络技术的运用
网络安全法宣贯标语
数据库的安全性的思维导图
python网上爬取数据库
国家制定了网络安全法了