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

Spring Cloud Feign's method of using object parameters

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

Share

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

This article Xiaobian for you to introduce in detail the "Spring Cloud Feign method of using object parameters", the content is detailed, the steps are clear, the details are handled properly, I hope this "Spring Cloud Feign method of using object parameters" article can help you solve your doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Overview

Spring Cloud Feign is used for the encapsulation of micro-services, which makes it easy to invoke micro-services through the implementation of interface proxy, and makes the use of micro-services like local services. But it is not perfect in transmitting parameters. When using Feign to proxy GET requests, there is no difficulty in using simple parameters (basic types, wrappers, strings), but it is impossible to automatically parse the fields contained in the object when passing parameters.

If you don't have the patience to read it, just skip to the last title and follow the action.

@ RequestBody

Object passing parameters is a very common operation, although it can be replaced by passing parameters one by one, but that would be too troublesome, so this problem must be solved.

I saw someone annotate object parameters with @ RequestBody on the Internet, and I tried it and found that it did work. This solution actually uses the body body with parameters (using GET requests), but there are some problems with this scheme:

Annotations need to be available on both consumer and provider, which causes trouble

Using the interface testing tool Postman was unable to run the microservice, but later found that it was due to the incorrect format of the body body, which was not the usual form or path stitching, but GraphQL. I haven't studied how parameters should be filled in in this format, but Postman doesn't give you a format as convenient as a form, which is bad for testing.

@ SpringQueryMap

So I continued to look for answers and found that you could use @ SpringQueryMap to automatically encode Map type parameters and splice them into URL simply by adding them to the parameters of consumer. The high-end version of Feign that I use can encode objects directly.

But just when I thought I was getting the right answer, I found that there was still a problem:

I obviously added @ DateTimeFormat (pattern = "yyyy-MM-dd") to a field of type Date, but it didn't work. He encoded (or serialized) in his own way, and the official did not provide this format.

After another search, I found that a boss himself has implemented an annotation transformation to replace @ SpringQueryMap, and has achieved a rich formatting function, ORZ. But I don't have that kind of technology, and I don't really want to copy and paste his pile of code, because if something goes wrong, it's hard to fix it, so I still want to make the maximum use of the framework and fill the hole in the framework to the minimum.

QueryMapEncoder

Finally, without effort, I found the custom encoder interface QueryMapEncoder reserved by Feign, and the framework provides two implementations:

FieldQueryMapEncoder

BeanQueryMapEncoder

Although these two implementations can not meet my requirements, I only need to write my own implementation class with a little modification, so I modified it on the basis of FieldQueryMapEncoder and added only one method to achieve the function.

Principle: Feign is actually encoded with Map, and the coding method is also very simple. String is key,Object and value. The initial approach is to encode the parameters with Object's toString () method, which is why the Date field becomes a default time format, because toString () has nothing to do with @ DateTimeFormat at all. The higher version uses an encoder to pass parameters to the object, which actually obtains the object's metadata through simple reflection and then puts it into the Map.

The above principles can be answered by the @ DateTimeFormat comments and the source code of the encoder.

All we have to do is customize an encoder to change the field into the string we want before the metadata is put into the Map. The following is the code I implemented for reference:

Package com.example.billmanagerfront.config.encoder;import java.lang.reflect.Field;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Collections;import java.util.List;import java.util.Map;import java.util.Optional;import java.util.TimeZone;import java.util.concurrent.ConcurrentHashMap;import java.util.stream.Collectors;import org.springframework.format.annotation.DateTimeFormat;import feign.Param;import feign.QueryMapEncoder;import feign.codec.EncodeException Public class PowerfulQueryMapEncoder implements QueryMapEncoder {private final Map

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