In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "openfeign get request parameter dto error how to solve", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "openfeign get request parameter dto error how to solve it"!
Spring boot 2.3.3 is used in the project.
Use @ GetMapping and @ SpringQueryMap and transport DTO objects when invoking the service using feign, where the DTO object contains the LocalDateTime property, keeps reporting type conversion exceptions, and cannot invoke the service. There are many solutions, many online solutions are ineffective, generally are FastJson serialization and so on (maybe each project is different), solution process analysis will not be analyzed for the time being. Record it first, because it seems that many people on the Internet have encountered this problem. The following is the service provider
@ FeignClient (value = "user-service", path = "/ user/v1") public interface UserClient {@ GetMapping ("/") PageVO getUserList (@ SpringQueryMap UserDTO userDTO);} @ Data@ApiModel (value = "operating platform user list query parameters") public class UserDTO implements Serializable {private static final long serialVersionUID =-3767202379100110105L; @ ApiModelProperty (value = "user id") private Long id @ Size (max = 12, message = "nickName: maximum length of user nicknames is 12") @ ApiModelProperty (value = "user nicknames") private String nickName; @ ApiModelProperty (value = "Mobile number") private String phone; @ DateTimeFormat (pattern = "yyyy-MM-dd HH:mm:ss") @ ApiModelProperty (value = "creation time") private LocalDateTime createdAtStart @ DateTimeFormat (pattern = "yyyy-MM-dd HH:mm:ss") @ ApiModelProperty (value = "creation time ends") private LocalDateTime createdAtEnd; @ ApiModelProperty (value = "page number", required = true) private Integer page; @ ApiModelProperty (value = "number per page", required = true) private Integer size;}
Core focus: add a 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 > (); 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);} at this point, I believe you have a better understanding of "how to solve errors in the openfeign get request parameter dto". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.