您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“openfeign get請(qǐng)求參數(shù)dto出現(xiàn)錯(cuò)誤怎么解決”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“openfeign get請(qǐng)求參數(shù)dto出現(xiàn)錯(cuò)誤怎么解決”吧!
項(xiàng)目中使用的是spring boot 2.3.3,spring-cloud Hoxton.SR8.
在使用feign調(diào)用服務(wù)時(shí) 使用@GetMapping 和 @SpringQueryMap 和傳輸DTO對(duì)象,其中DTO對(duì)象中包含LocalDateTime屬性,一直報(bào)類(lèi)型轉(zhuǎn)換異常,無(wú)法調(diào)用服務(wù)。解決方法有很多,找了網(wǎng)上很多解決辦法都沒(méi)效果,大體都是FastJson 序列化之類(lèi)的(可能每個(gè)項(xiàng)目差異吧), 解決過(guò)程分析暫不分析吧。先行記錄一下,因?yàn)榭吹骄W(wǎng)上很多人貌似都遇到過(guò)這個(gè)問(wèn)題。以下是服務(wù)提供方
@FeignClient(value = "user-service", path = "/user/v1") public interface UserClient { @GetMapping("/") PageVO<UserVO> getUserList(@SpringQueryMap UserDTO userDTO); }
@Data @ApiModel(value = "運(yùn)營(yíng)平臺(tái)用戶(hù)列表查詢(xún)參數(shù)") public class UserDTO implements Serializable { private static final long serialVersionUID = -3767202379100110105L; @ApiModelProperty(value = "用戶(hù)id") private Long id; @Size(max = 12, message = "nickName:用戶(hù)昵稱(chēng)最大長(zhǎng)度為12") @ApiModelProperty(value = "用戶(hù)昵稱(chēng)") private String nickName; @ApiModelProperty(value = "手機(jī)號(hào)碼") private String phone; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @ApiModelProperty(value = "創(chuàng)建時(shí)間開(kāi)始") private LocalDateTime createdAtStart; @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @ApiModelProperty(value = "創(chuàng)建時(shí)間結(jié)束") private LocalDateTime createdAtEnd; @ApiModelProperty(value = "頁(yè)碼", required = true) private Integer page; @ApiModelProperty(value = "每頁(yè)條數(shù)", required = true) private Integer size; }
核心重點(diǎn):新增一個(gè)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<Class<?>, ObjectParamMetadata> classToMetadata = new HashMap<Class<?>, ObjectParamMetadata>(); private final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); @Override public Map<String, Object> encode(Object object) throws EncodeException { try { ObjectParamMetadata metadata = getMetadata(object.getClass()); Map<String, Object> propertyNameToValue = new HashMap<String, Object>(); 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<PropertyDescriptor> objectProperties; private ObjectParamMetadata(List<PropertyDescriptor> objectProperties) { this.objectProperties = Collections.unmodifiableList(objectProperties); } private static ObjectParamMetadata parseObjectType(Class<?> type) throws IntrospectionException { List<PropertyDescriptor> properties = new ArrayList<PropertyDescriptor>(); 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); } } }
@Configuration public class CustomFeignConfig { @Bean public Feign.Builder feignBuilder() { return Feign.builder() .queryMapEncoder(new LocalDateTimeQueryMapEncoder()) .retryer(Retryer.NEVER_RETRY); } }
到此,相信大家對(duì)“openfeign get請(qǐng)求參數(shù)dto出現(xiàn)錯(cuò)誤怎么解決”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。