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

The principle and function of RequestInterceptor in feign

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article mainly explains "the principle and function of RequestInterceptor in feign". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "the principle and function of RequestInterceptor in feign".

Order

This paper mainly studies the RequestInterceptor of feign.

RequestInterceptor

FeignMuscoreMush10.2.3Muthsources.jarhammer Universe RequestInterceptor.java

Public interface RequestInterceptor {/ * * Called for every request. Add data using methods on the supplied {@ link RequestTemplate}. * / void apply (RequestTemplate template);}

The RequestInterceptor interface defines the apply method, whose parameter is RequestTemplate;. It has an abstract class called BaseRequestInterceptor, and several implementation classes are BasicAuthRequestInterceptor, FeignAcceptGzipEncodingInterceptor, FeignContentGzipEncodingInterceptor.

BasicAuthRequestInterceptor

FeignMuscoreMush10.2.3Muthsources.jarroommax FigignAccord authbank BasicAuthRequestInterceptor.java

Public class BasicAuthRequestInterceptor implements RequestInterceptor {private final String headerValue; / * * Creates an interceptor that authenticates all requests with the specified username and password * encoded using ISO-8859-1. * * @ param username the username to use for authentication * @ param password the password to use for authentication * / public BasicAuthRequestInterceptor (String username, String password) {this (username, password, ISO_8859_1);} / * * Creates an interceptor that authenticates all requests with the specified username and password * encoded using the specified charset. * * @ param username the username to use for authentication * @ param password the password to use for authentication * @ param charset the charset to use when encoding the credentials * / public BasicAuthRequestInterceptor (String username, String password, Charset charset) {checkNotNull (username, "username"); checkNotNull (password, "password"); this.headerValue = "Basic" + base64Encode ((username + ":" + password) .getBytes (charset));} / * This uses a Sun internal method If we ever encounter a case where this method is not * available, the appropriate response would be to pull the necessary portions of Guava's * BaseEncoding class into Util. * / private static String base64Encode (byte [] bytes) {return Base64.encode (bytes);} @ Override public void apply (RequestTemplate template) {template.header ("Authorization", headerValue);}}

BasicAuthRequestInterceptor implements the RequestInterceptor interface, and its apply method adds a header named Authorization to the RequestTemplate

BaseRequestInterceptor

Springhouse cloudhouse openfeignlycoreMuray 2.2.0.M1ripsources.jarroomUniorgAccording to springframeworkqqcloudLigopenfeignincencodinglapBaseRequestInterceptor.java

Public abstract class BaseRequestInterceptor implements RequestInterceptor {/ * * The encoding properties. * / private final FeignClientEncodingProperties properties; / * * Creates new instance of {@ link BaseRequestInterceptor}. * @ param properties the encoding properties * / protected BaseRequestInterceptor (FeignClientEncodingProperties properties) {Assert.notNull (properties, "Properties can not be null"); this.properties = properties;} / * Adds the header if it wasn't yet specified. * @ param requestTemplate the request * @ param name the header name * @ param values the header values * / protected void addHeader (RequestTemplate requestTemplate, String name, String... Values) {if (! requestTemplate.headers () .containsKey (name)) {requestTemplate.header (name, values);}} protected FeignClientEncodingProperties getProperties () {return this.properties;}}

BaseRequestInterceptor defines the addHeader method to add a non-duplicated header to the requestTemplate

FeignAcceptGzipEncodingInterceptor

Springhouse 2.2.0.M1lysources.jarl.com orgAcceptGzipEncodingInterceptor.java

Public class FeignAcceptGzipEncodingInterceptor extends BaseRequestInterceptor {/ * * Creates new instance of {@ link FeignAcceptGzipEncodingInterceptor}. * @ param properties the encoding properties * / protected FeignAcceptGzipEncodingInterceptor (FeignClientEncodingProperties properties) {super (properties) } / * @ inheritDoc} * / @ Override public void apply (RequestTemplate template) {addHeader (template, HttpEncoding.ACCEPT_ENCODING_HEADER, HttpEncoding.GZIP_ENCODING, HttpEncoding.DEFLATE_ENCODING);}}

FeignAcceptGzipEncodingInterceptor inherits BaseRequestInterceptor, and its apply method adds a header named Accept-Encoding with a value of gzip,deflate to RequestTemplate

FeignContentGzipEncodingInterceptor

Springhouse 2.2.0.M1lysources.jarwithAccording to orgAccording frameworkGetWork cloudlyopenFefeignContentGzipEncodingInterceptor.java

Public class FeignContentGzipEncodingInterceptor extends BaseRequestInterceptor {/ * * Creates new instance of {@ link FeignContentGzipEncodingInterceptor}. * @ param properties the encoding properties * / protected FeignContentGzipEncodingInterceptor (FeignClientEncodingProperties properties) {super (properties) } / * * {@ inheritDoc} * / @ Override public void apply (RequestTemplate template) {if (requiresCompression (template)) {addHeader (template, HttpEncoding.CONTENT_ENCODING_HEADER, HttpEncoding.GZIP_ENCODING, HttpEncoding.DEFLATE_ENCODING) }} / * Returns whether the request requires GZIP compression. * @ param template the request template * @ return true if request requires compression, false otherwise * / private boolean requiresCompression (RequestTemplate template) {final Map headers = template.headers (); return matchesMimeType (headers.get (HttpEncoding.CONTENT_TYPE)) & & contentLengthExceedThreshold (headers.get (HttpEncoding.CONTENT_LENGTH)) } / * Returns whether the request content length exceed configured minimum size. * @ param contentLength the content length header value * @ return true if length is grater than minimum size, false otherwise * / private boolean contentLengthExceedThreshold (Collection contentLength) {try {if (contentLength = = null | | contentLength.size ()! = 1) {return false } final String strLen = contentLength.iterator (). Next (); final long length = Long.parseLong (strLen); return length > getProperties (). GetMinRequestSize ();} catch (NumberFormatException ex) {return false }} / * Returns whether the content mime types matches the configures mime types. * @ param contentTypes the content types * @ return true if any specified content type matches the request content types * / private boolean matchesMimeType (Collection contentTypes) {if (contentTypes = = null | | contentTypes.size () = = 0) {return false } if (getProperties (). GetMimeTypes () = = null | | getProperties () .getMimeTypes () .length = = 0) {/ / no specific mime types has been set-matching everything return true } for (String mimeType: getProperties (). GetMimeTypes ()) {if (contentTypes.contains (mimeType)) {return true;}} return false;}}

FeignContentGzipEncodingInterceptor inherits BaseRequestInterceptor. Its apply method first determines whether compression is needed, that is, whether mimeType meets the requirement and whether content size exceeds the threshold. If compress is needed, add header named Content-Encoding and value gzip,deflate.

Summary

The RequestInterceptor interface defines the apply method, whose parameter is RequestTemplate;. It has an abstract class called BaseRequestInterceptor, and several implementation classes are BasicAuthRequestInterceptor, FeignAcceptGzipEncodingInterceptor, FeignContentGzipEncodingInterceptor.

BasicAuthRequestInterceptor implements the RequestInterceptor interface, and its apply method adds a header named Authorization to the RequestTemplate

BaseRequestInterceptor defines the addHeader method, which adds a non-duplicated header;FeignAcceptGzipEncodingInterceptor to requestTemplate and inherits BaseRequestInterceptor. Its apply method adds a header;FeignContentGzipEncodingInterceptor named Accept-Encoding to RequestTemplate, and a header;FeignContentGzipEncodingInterceptor with a value of gzip,deflate inherits BaseRequestInterceptor. Its apply method first determines whether compression is needed, that is, whether mimeType meets the requirements and whether the content size exceeds the threshold. If compress is needed, a header named Content-Encoding and gzip,deflate is added.

Thank you for reading, the above is the content of "the principle and function of RequestInterceptor in feign". After the study of this article, I believe you have a deeper understanding of the principle and function of RequestInterceptor in feign, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

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