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 use Feign to transmit date type parameters in SpringCloud

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

SpringCloud how to use Feign to transmit date type parameters, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can get something.

I. the problem of time difference 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 2019date2: 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.

II. The problem of 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;} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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

Internet Technology

Wechat

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

12
Report