In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use feign call to return object type to convert into entity, the article is very detailed, has a certain reference value, interested friends must read it!
Feign call returns object to entity com.fasterxml.jackson.core jackson-databind 2.9.8ObjectMapper mapper=new ObjectMapper (); CourseMaster courseMaster = mapper.convertValue (bean,CourseMaster.class); List list= (List) courseMasterList;// returns Object type for (Object cs:list) {CourseMaster courseMaster = mapper.convertValue (cs,CourseMaster.class);} feign call reports type conversion error phenomenon
Call between springcloud feign services, run to the following figure 1 code An and report an error:
Java.util.LinkedHashMap cannot be cast to (error reported as shown in figure 2 below)
Investigation process
Step1: in most cases, the first reaction is to check the syntax carefully and make sure that the lambda expression itself is fine
Step2: after confirming that there is no problem with writing, take out the code snippet and write a main method to run (as shown in figure 1 code snippet B) and find that it works well when taken out alone in the main method!
Step3: get confused at this time, so re-read the error message: LinkedHashMap cannot be cast to xxx.BaseComapny, you can clearly determine that it is a type conversion error.
Look at the code line by line (snippet An in figure 1) and find that there is only a possible type conversion in the section: List list = xxxxx above snippet A, so navigate to this line of code for the time being.
The reason for the uncertainty in step4:step3 is that the responseObject in this line of code is obtained directly through the service call, and the object type is specified in the request response parameters, so I don't think there is a cast or LinkedHashMap here.
Continue to speculate that if there is a problem, it is only possible that the return parameters obtained through the service call are not consistent with the expectations. Step5: so debug code An and code B (that is, the main method) in figure 1 above, respectively. As shown in the figure below, it is found that the response parameters obtained after the actual service call are inconsistent with those in the main method. You can see that the response parameter list obtained from the snippet A service call is indeed LinkedHashMap! At this point, I found the problem!
Step6: because you are sure that you have not specially handled the response parameters before and after the service invocation, so consider that this is a framework behavior, and basically find the cause of this problem.
Cause of the problem
After finding out the reason, the targeted Baidu answer is easier to get. To put it simply, when you use feign to make a remote service call, the return parameters (in my code, the list part of Response) will become LinkedHashMap.
Here are the most popular explanations that can be found online (because there are so many articles and blogs that copy this passage, it is impossible to identify who copied whom or who the first author was written, so the original author cannot be identified):
This is because RPC remote calls use HTTPClient at the bottom, so there must be an order when passing parameters, and the values in Map must be in order when you pass Map, otherwise there will be something wrong with the service layer, so it will change from Map to LinkedHashMap! Spring has a class called ModelMap, which inherits LinkedHashMap, so the result returned by an API can be directly connected with ModelMap. Note that ModelMap does not have generics. No matter what type of Map you return and how complex the generics are, you can directly new a ModelMap and use it to receive the returned results.
Solution.
1. The simplest solution is to receive the response parameters directly with LinkedHashMap, and get the parameters in kv form.
2. Convert LinkedHashMap into the object you want by json,objectmapper, etc.
3. Customize the response parameters obtained from the feign call (maybe? )
The above is all the content of the article "how to use feign calls to return object types to convert to entities". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.