本篇內(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ò)誤怎么解決”吧!
按需搭建網(wǎng)站可以根據(jù)自己的需求進(jìn)行定制,成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作構(gòu)思過(guò)程中功能建設(shè)理應(yīng)排到主要部位公司成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作的運(yùn)用實(shí)際效果公司網(wǎng)站制作網(wǎng)站建立與制做的實(shí)際意義
項(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("/") PageVOgetUserList(@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, 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); } } }
@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í)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!