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 realize dynamic URL with Spring Cloud Feign

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Most people do not understand the knowledge points of this article "Spring Cloud Feign how to achieve dynamic URL", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Spring Cloud Feign how to achieve dynamic URL" article.

Requirement description

Requirements scenarios for dynamic URL:

There is an asynchronous service S, which provides asynchronous service interfaces for other businesses (Business A, Business B.). After the specified logic is executed in these asynchronous interfaces, the interfaces of the corresponding business parties need to be called back.

This is common in scenarios such as risk control audits and payment callbacks.

So, how should this callback business side interface be implemented?

First of all, you need to agree on the request method (usually POST request), request parameter format (usually JSON format for easy extension) and response message format (usually JSON format) when calling back these business side interfaces.

There are two ways to call the business side interface:

1. Write a set of callback logic independently in each asynchronous interface of service S

two。 Because the method type and parameter format of the callback are agreed upon, you can write a unified common callback method.

Method 1 is obviously not the best choice, which results in a lot of repetitive code logic and makes very little use of post-maintenance and upgrades.

The implementation of method 2 is more flexible and easy to expand.

How to define a common callback method using the Feign framework is described below.

Concrete realization

The basis for implementing dynamic URL in Feign is the support of the framework itself. Just include a java.net.URI parameter in the interface method, and Feign will use that parameter as the target host address, as detailed in the "Overriding the Request Line" section of the Interface Annotations section.

The following describes how to define a unified callback using Feign independently and using Spring Cloud OpenFeign respectively.

Define a unified callback method using Feign

Define a unified callback method:

Public interface CallbackAPI {/ * unified callback API method. The request message body format is JSON, and the response message body format is also JSON * @ param host interface host address, such as http://localhost:8080. This parameter is the key * @ param path interface path to implement dynamic URL. For example: / test/hello * @ param queryMap dynamic URL parameter collection * @ param body request message body object * @ return * / @ RequestLine ("POST {path}") @ Headers ({"Content-Type: application/json", "Accept: application/json"}) Object callback (URI host, @ Param ("path") String path, @ QueryMap Map queryMap, Subject body) / * Unified callback API method. The request message body format is JSON, and the response message body format is also the complete request path address of JSON * @ param uri. For example: http://localhost:8080/test/hello * @ param queryMap dynamic URL parameter collection * @ param body request message body object * @ return * / @ RequestLine ("POST") @ Headers ({"Content-Type: application/json", "Accept: application/json"}) Object callback (URI uri, @ QueryMap Map queryMap, Subject body) }

Call the callback method:

CallbackAPI callbackAPI = Feign.builder () .encoder (new GsonEncoder ()) .decoder (new GsonDecoder ()) .logger (new Slf4jLogger ()) .logLevel (Logger.Level.FULL) .target (CallbackAPI.class, "EMPTY"); / / Note: the url parameter here cannot be an empty string, but it can be set to any string value, which is set to "EMPTY" String uri = "http://localhost:8080";" here. Map queryMap = new HashMap (0); queryMap.put ("k", "v"); Subject body = Subject.builder (). Id (10). Build (); / / request that the host address be passed separately from the path to callbackAPI.callback (URI.create (uri), "/ test/simple/post/json", queryMap, body) / / directly take the full request full path as the uri type parameter callbackAPI.callback ("http://localhost:8080/test/simple/post/json"), queryMap, body)

The detailed request log is as follows:

2022-02-14 15 main 32 main 13118 DEBUG [main] f.Logger [Slf4jLogger.java:72] [CallbackAPI#callback]-> POST http://localhost:8080/test/simple/post/json?k=v HTTP/1.1

2022-02-14 15 f.Logger 3218 DEBUG [main] f.Logger [Slf4jLogger.java:72] [CallbackAPI#callback] Accept: application/json

2022-02-14 15 f.Logger 3218 DEBUG [main] f.Logger [Slf4jLogger.java:72] [CallbackAPI#callback] Content-Length: 14

2022-02-14 15 f.Logger 3218 DEBUG [main] f.Logger [Slf4jLogger.java:72] [CallbackAPI#callback] Content-Type: application/json

2022-02-14 15 f.Logger 3218 DEBUG [main] f.Logger [Slf4jLogger.java:72] [CallbackAPI#callback]

2022-02-14 15 f.Logger 3218 DEBUG [main] f.Logger [Slf4jLogger.java:72] [CallbackAPI#callback] {

"id": 10

}

2022-02-14 15 f.Logger 3218 DEBUG [main] f.Logger [Slf4jLogger.java:72] [CallbackAPI#callback]-> END HTTP (14-byte body)

2022-02-14 15 f.Logger [main] f.Logger [Slf4jLogger.java:72] [CallbackAPI#callback] END HTTP (14-byte body)

2022-02-14 15 s.n.www.protocol.http.HttpURLConnection 38 DEBUG 20184-[main] s.n.www.protocol.http.HttpURLConnection: sun.net.www.MessageHeader@539c48307 pairs: {POST / test/simple/post/json?k=v HTTP/1.1: null} {Accept: application/json} {Content-Type: application/json} {User-Agent: Java/1.8.0_271} {Host: localhost:8080} {Connection: keep-alive} {Content-Length: 14}

2022-02-14 15 Feb 38 DEBUG 38.945 s.n.www.protocol.http.HttpURLConnection: sun.net.www.MessageHeader@681e913c6 pairs: {null: HTTP/1.1 20184} {Content-Type: application/json} {Content-Length: 9} {Date: Mon, 14 Feb 2022 07:38:38 GMT} {Keep-Alive: timeout=60} {Connection: keep-alive}

2022-02-14 15 feign.Logger 38.952 DEBUG 20184-[main] feign.Logger: [CallbackAPI#callback]

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