Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of error in Date type parameters of SpringCloud Feign transmission

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail how to solve the problem of errors in SpringCloud Feign transmission Date type parameters. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Error exists in Date type parameters of Feign transmission

Recently, in the process of project development, the time passed by the front end (Date type) is correct in module A, and then module A calls module B to pass the time (Date type) as a parameter, and then module B receives an error in the time, which will be one more day and 10 hours less, which should be caused by the SpringCloud Feign component.

My solution here is to change the time (Date type) to String type before A module is called, and B module can change the time from String type to Date type after receiving the parameters of A module.

The time conversion code is as follows: / * date is formatted as a string * * @ param source * @ return java.lang.String * @ author zxzhang * @ date 2020-2-9 * / public Date string2date (String source) throws ParseException {SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); return sdf.parse (source) } / * string parses to date * * @ param source * @ return java.lang.String * @ author zxzhang * @ date 2020-2-9 * / public String date2String (Date source) {SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); return sdf.format (source);}

At this point, the problem of errors in Date type parameters in Feign transmission is perfectly solved.

Feign transmits date type parameters with a time difference of 14 hours. Time difference problem of Java Date type

Please look at the following code

Public static void main (String [] args) throws Exception {Date date1 = new Date (); System.out.println ("date1:" + date1.toString ()); Date date2 = new Date (date1.toString ()); System.out.println ("date2:" + date2.toString ());}

The execution result is as follows

Date1: Mon Jul 22 08:47:19 CST 2019

Date2: Mon Jul 22 22:47:19 CST 2019

The current time is 08:48 on July 22, 2019. CST is short for China's time zone China Standard Time, but you can see that the input of date2 is 14 hours longer than the actual time.

CTS actually represents four time zones (Central Standard Time (USA) UT-6:00, Central Standard Time (Australia) UT+9:30, China Standard Time UT+8:00, Cuba Standard Time UT-4:00), and represents the standard time of the United States, Australia, China and Cuba.

Reason

New Date (date1.toString ())

This method calls the Date.parse (String) method, which passes an argument of Mon Jul 22 08:47:19 CST 2019, with a comment on this method

* Any word that matches EST, CST, MST, or PST,* ignoring case, is recognized as referring to the time zone in* North America that is five, six, seven, or eight hours west of* Greenwich, respectively. Any word that matches EDT, CDT,* MDT, or PDT, ignoring case, is recognized as* referring to the same time zone, respectively, during daylight* saving time.

You can see that CST will be treated as the time zone Central Standard Time of the central United States, that is, JVM thinks that the time you passed is the time of the central United States, and when date2 calls the toString method, it will detect that the system's time zone is China, and it will automatically add 14 hours (the time difference between East eight and West six), and it will become Mon Jul 22 22:47:19 CST 2019.

Solution method

In fact, this problem is very difficult to appear if you write your own code, because all Java books are not taught this way, and most of them convert Date to String through SimpleDateFormat. After all, the method new Date (date1.toString ()) has been marked as obsolete.

Problems with Feign client

When the Feign client communicates, it will call the toString method of Date to change to the String type. When the server accepts it, it uses the new Date (String) method. Here, the problem described above will occur, resulting in a 14-hour jet lag.

Solution method

Add code on the client side to specify that Feign converts the Date parameter to the format of the String parameter:

Import lombok.extern.slf4j.Slf4j;import org.springframework.cloud.openfeign.FeignFormatterRegistrar;import org.springframework.core.convert.converter.Converter;import org.springframework.format.FormatterRegistry;import org.springframework.stereotype.Component;import java.text.SimpleDateFormat;import java.util.Date; @ Slf4j@Componentpublic class FeignDateFormatRegister implements FeignFormatterRegistrar {@ Override public void registerFormatters (FormatterRegistry registry) {registry.addConverter (Date.class, String.class, new Date2StringConverter ()) } private class Date2StringConverter implements Converter {@ Override public String convert (Date source) {SimpleDateFormat sdf = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); return sdf.format (source);}

Add code on the server side to specify the converter used by SpringContext in String and Date, and let the converter know the parameter format we configured on the client side:

Import lombok.extern.slf4j.Slf4j;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Configuration;import org.springframework.core.convert.converter.Converter;import org.springframework.core.convert.support.GenericConversionService;import org.springframework.web.bind.support.ConfigurableWebBindingInitializer;import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; import javax.annotation.PostConstruct;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Date @ Slf4j@Configurationpublic class FeignConfiguration {@ Autowired private RequestMappingHandlerAdapter handlerAdapter; / * add string to date function * / @ PostConstruct public void initEditableValidation () {ConfigurableWebBindingInitializer initializer = (ConfigurableWebBindingInitializer) handlerAdapter.getWebBindingInitializer (); if (initializer.getConversionService ()! = null) {GenericConversionService genericConversionService = (GenericConversionService) initializer.getConversionService () GenericConversionService.addConverter (String.class, Date.class, new String2DateConverter ());}} class String2DateConverter implements Converter {@ Override public Date convert (String source) {SimpleDateFormat simpleDateFormat = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss"); try {return simpleDateFormat.parse (source) } catch (ParseException e) {log.error ("", e);} return null;}

Note that the above two configuration classes need to configure package scanning and the like to add them to the Spring environment

This is the end of this article on "how to solve the problem of errors in SpringCloud Feign transmission Date type parameters". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report