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 pit of SpringBoot integrating OpenFeign

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

Share

Shulou(Shulou.com)05/31 Report--

This article Xiaobian for you to introduce in detail "SpringBoot integration OpenFeign pit how to solve", the content is detailed, the steps are clear, the details are handled properly, I hope this "SpringBoot integration OpenFeign pit how to solve" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge.

Project Integration OpenFegin

Integrated 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.

@ Configurationpublic 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 {/ * cache all Fegin clients * / private volatile static Map feginClientCache = new HashMap () / * get 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) @ SpringBootTestpublic class IcpsWeightStarterTest {@ Testpublic void testUserLogin () {ResponseMessage result = FeginClientFactory.getFeginClient (FeginClientProxy.class, "http://127.0.0.1").login(new UserLoginVo (" zhangsan "," 123456 ", 1)); 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; solves the problem

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:04:32")

Analysis of problems

Calling fegin from the client is equivalent to URL passing parameters is equivalent to a JSON conversion, the database takes out '2020-10-07T11:04:32' data at this time is the time type, after entering the JSON becomes the String type, T becomes the character is no longer a special character, so the String string "2020-10-07T11:04:32" deserialization 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 this, the article "how to solve the pit of SpringBoot integrating OpenFeign" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, 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.

Share To

Internet Technology

Wechat

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

12
Report