In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how to solve the error of calling date type conversion between springcloud feign services". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how to solve the error of calling date type conversion between springcloud feign services".
Call between feign services, date type conversion error
Recently I tried to switch to springcloud development, which used to be springboot, and each time the Date type of the return value was formatted yyyy-MM-dd HH:mm:ss with @ ControllerAdvice and then returned. After using feign this time, the calls between the two services have been reported by Baidu for a long time, and then there was a flash of inspiration. Don't say much, go to the plan.
Custom feign request header
Identifies that only a feign request will have an extra flag header
@ Configurationpublic class FeignHeaderConfig implements RequestInterceptor {@ Override public void apply (RequestTemplate requestTemplate) {ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder .getRequestAttributes (); HttpServletRequest request = attributes.getRequest (); String token = request.getHeader (C.API_HEADER_PARAM_TOKEN); String session = request.getHeader (C.API_HEADER_PARAM_SESSION); String timestamp = request.getHeader (C.API_HEADER_PARAM_TIMESTAMP) RequestTemplate.header (C.API_HEADER_PARAM_TOKEN, token); requestTemplate.header (C. API session); requestTemplate.header (C. API request, "feign"); requestTemplate.header ("flag", "feign"); / / identify feign request}} by determining whether it is an feign request
And then decide whether you need formatting time.
@ ControllerAdvice@Slf4jpublic class RespondeBodyConfig implements ResponseBodyAdvice {@ Override public Object beforeBodyWrite (Object resBody, MethodParameter arg1, MediaType arg2, Class arg3, ServerHttpRequest req, ServerHttpResponse res) {/ / resolves the formatting of calls between feign, resulting in a date type conversion error List flag = req.getHeaders () .get ("flag") If (flag! = null & & flag.size () > 0 & & flag.get (0). Equals ("feign")) {return resBody;} / / resBody is the value returned in the controller method. Modify it and then return it to ObjectMapper mapper = new ObjectMapper (); SimpleDateFormat format = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); mapper.setDateFormat (format) Try {if (resBody instanceof TResult) {String json = mapper.writeValueAsString (resBody); return JSONObject.parseObject (json, TResult.class);} catch (Exception e) {log.error ("[global return value processing failed]", e);} return resBody } @ Override public boolean supports (MethodParameter arg0, Class arg1) {/ / return true directly here, indicating that the beforeBodyWrite method return true;}} is called on the responsebody of any handler when the date format exception is called between OpenFeign services
Com.fasterxml.jackson.databind.exc.InvalidFormatException: Cannot deserialize value of type `java.util.Date` from String "2021-07-27 10:35:16": not a valid representation (error: Failed to parse Date value '2021-07-27 10 purge 35 purge 16: Cannot parse date "2021-07-27 10:35:16": while it seems to fit format' yyyy-MM-dd'T'HH:mm:ss.SSSZ', parsing fails (leniency? Null))
Reason
When using OpenFeign to make inter-service calls, the time type returned in the object is string type, and the corresponding type of received object is Date type. An exception occurs when openFeign converts the date format.
Solution method
1. Add to the configuration file
Spring.jackson.timeZone=GMT+08:00spring.jackson.date-format=yyyy-MM-dd HH:mm:ssspring.jackson.serialization.write_dates_as_timestamps=falsespring: jackson: timeZone: GMT+08:00 date-format: yyyy-MM-dd HH:mm:ss serialization.write_dates_as_timestamps: false
2. Add a note to the field
@ JsonFormat (pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8") private Date createTime; thank you for reading. The above is the content of "how to solve the error of calling date type conversion between springcloud feign services". After the study of this article, I believe you have a deeper understanding of how to solve the error of calling date type conversion between springcloud feign services, and the specific usage needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.