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

What should I do if an exception occurs in the return value LocalDateTime via OpenFeign request

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

It is believed that many inexperienced people are at a loss about what to do if an exception occurs in the return value LocalDateTime through the OpenFeign request. Therefore, this article summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

Write at the front

Recently, I have been using SpringBoot+K8S to develop micro-service systems, and now that I use K8S, I don't want to use SpringCloud. Why, because K8S itself provides very 6 technologies that micro-services need to use, such as service registration and discovery, current limit, circuit breaker, load balancing and so on, then why should I connect to SpringCloud? Er, having said so much, I will encounter some problems when I really use the technology stack of SpringBoot+K8S. For example, when I do not need to use SpringCloud, when I call other services, I use native OpenFegin, and when I use OpenFegin to call other services, I encounter a big hole. An exception has occurred in the return value LocalDateTime through the OpenFeign request. Today, let's talk about this pit!

Project Integration OpenFegin Integration OpenFegin dependency

First of all, let me tell you about the configuration of the project. The overall project uses version 2.2.6 of SpringBoot and the native OpenFegin uses 11.0. we introduce OpenFegin into pom.xml in the following ways.

UTF-8

False

1.8

11.0

Io.github.openfeign

Feign-core

${openfegin.version}

Io.github.openfeign

Feign-jackson

${openfegin.version}

Here, I omitted some other configuration items.

Next, I started using OpenFegin to invoke remote services in my project. The specific steps are as follows.

Implement remote call

First, create the OpenFeignConfig class and configure the Contract that OpenFegin uses by default.

@ Configuration

Public class OpenFeignConfig {

@ Bean

Public Contract useFeignAnnotations () {

Return new Contract.Default ()

}

}

Next, we write a general factory class to obtain the OpenFeign client. This class is also relatively simple. In essence, it uses a HashMap to cache all the FeginClient. The FeginClient of this is essentially our custom Fegin interface, the Key in the cache is the basic URL for the request connection, and the cached Value is the FeginClient interface we defined.

Public class FeginClientFactory {

/ * *

* caching all Fegin clients

, /

Private volatile static Map feginClientCache = new HashMap ()

/ * *

* caching the FeginFegin client to Map and getting data from Map

* @ return

, /

@ SuppressWarnings ("unchecked")

Public static T getFeginClient (Class clazz, String baseUrl) {

If (! feginClientCache.containsKey (baseUrl)) {

Synchronized (FeginClientFactory.class) {

If (! feginClientCache.containsKey (baseUrl)) {

T feginClient = Feign.builder () .decoder (new JacksonDecoder ()) .encoder (new JacksonEncoder ()) .target (clazz, baseUrl)

FeginClientCache.put (baseUrl, feginClient)

}

}

}

Return (T) feginClientCache.get (baseUrl)

}

}

Next, we define a FeginClient interface.

Public interface FeginClientProxy {

@ Headers ("Content-Type:application/json;charset=UTF-8")

@ RequestLine ("POST / user/login")

UserLoginVo login (UserLoginVo loginVo)

}

Next, we create a test class for SpringBoot.

@ RunWith (SpringRunner.class)

@ SpringBootTest

Public class BingheStarterTest {

@ Test

Public void testUserLogin () {

ResponseMessage result = FeginClientFactory.getFeginClient (FeginClientProxy.class, "http://127.0.0.1").login(new UserLoginVo (" zhangsan "," 123456 "))

System.out.println (JsonUtils.bean2Json (result))

}

}

Everything is ready to run the test. Ma Dang, something's wrong. The main problem is that an exception occurs in the return value LocalDateTime field through the OpenFeign request!

Note: when an exception occurs at this time, the comments we add to the LocalDateTime field are as follows.

Import java.time.LocalDateTime

Import com.baomidou.mybatisplus.annotation.FieldFill

Import com.baomidou.mybatisplus.annotation.TableField

Import com.fasterxml.jackson.annotation.JsonFormat

@ TableField (value = "CREATE_TIME", fill = FieldFill.INSERT)

@ JsonFormat (pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")

Private LocalDateTime createTime

Problem solving problem description

SpringBoot calls the HTTP interface through the native OpenFeign client. If the return value contains the LocalDateTime type (including the time class of the java.time package in other JSR-310), a deserialization failure may occur on the client side. The error message is as follows:

Caused by:com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `java.time.LocalDateTime` (no Creators, like default construct, exist): no String-argument constructor/factory method to deserialize from String value ('2020-10-07T11 virtual 04pur32')

Analysis of problems

Calling fegin from the client is equivalent to URL passing parameters is equivalent to passing parameters after a JSON conversion, the database takes out '2020-10-07T11-04JSON 32' data at this time is the time type, after entering the JSON it becomes a String type, and T becomes a character that is no longer a special character, so the string "2020-10-07T11:04:32" deserialization of String will fail.

Problem solving

Increase dependencies in the project.

Com.fasterxml.jackson.datatype

Jackson-datatype-jsr310

2.9.9

Note: if you are using SpringBoot and explicitly specify the SpringBoot version, you do not have to specify the version number when introducing jackson-datatype-jsr310.

Next, add the following note to the LocalDateTime type field of the POJO class.

Import com.fasterxml.jackson.databind.annotation.JsonDeserialize

Import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer

The added effect is as follows.

Import java.time.LocalDateTime

Import com.baomidou.mybatisplus.annotation.FieldFill

Import com.baomidou.mybatisplus.annotation.TableField

Import com.fasterxml.jackson.annotation.JsonFormat

Import com.fasterxml.jackson.databind.annotation.JsonDeserialize

Import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer

@ TableField (value = "CREATE_TIME", fill = FieldFill.INSERT)

@ JsonFormat (pattern = "yyyy-MM-dd HH:mm:ss", locale = "zh", timezone = "GMT+8")

@ JsonDeserialize (using = LocalDateTimeDeserializer.class)

Private LocalDateTime createTime

At this point, the remote interface is called again and the problem is solved.

After reading the above, do you know what to do if an exception occurs in the return value LocalDateTime through the OpenFeign request? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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