您好,登錄后才能下訂單哦!
本篇內容主要講解“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("/") PageVO<UserVO> getUserList(@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<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); } }
到此,相信大家對“openfeign get請求參數dto出現錯誤怎么解決”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。