In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "how to use Feign to call a third-party http interface". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Feign calls the third-party http interface
When we are developing, we often encounter calling third-party interfaces. At this time, we can use httpClient or restTemplate, but compared with Feign calls, these two methods will be a little more troublesome.
Feign is a declarative service invocation client, which is both standardized and concise, helps us shield the complexity of http invocation, and perfectly cuts into the springcloud technology system.
Let's demonstrate it.
Use Feign to invoke a third-party client.
FeignClient (name = "my-feign-client", url = "http://**")public interface MyFeignClient {@ RequestMapping (value =" * * ", method = RequestMethod.GET) ResponseEntity getMsg (@ RequestParam (" token ") String token);}
Name should not be the same as the service name of the registry. Url is the address we want to request.
From the above, we can see that it is very simple, and serialization and other things have been done for us, there is no need for us to care, it is worth trying.
Native Feign calls third-party interfaces
Recently do a small project, small as only one page, and several interfaces only.
Because you are working on the project on your own, all definitions can be made by yourself.
The purpose of the project is to push the data collected to third-party pages, so docking is a priority.
Before docking with a variety of third-party SMS interfaces, I only used httpClient's get/post. If I use too much, I want to be familiar with another technology.
Introduce dependency
Don't say anything, introduce a bunch of things first, not every one is useful, but it's convenient to use it.
Core: it has to be cited, core dependency.
Gson/jackson:gson is used in my code to deal with json data.
Io.github.openfeign feign-core 9.5.0 com.netflix.feign feign-gson 8.18.0 io.github.openfeign feign-slf4j 9.5.0 io.github.openfeign feign-hystrix 9.5.0 io.github.openfeign feign-jackson 9.5.0 write interface
The following interface is free and public, so there should be no interest involved.
Package com.geewise.ddsp.phonecollect.api; import com.alibaba.fastjson.JSONObject;import feign.Param;import feign.RequestLine; import java.net.MalformedURLException / * Mobile number query area * / public interface DetailPhoneClient {/ * http://mobsec-dianhua.baidu.com/dianhua_api/open/location?tel=13322222222&qq-pf-to=pcqq.c2c * response: {"response": {"13322222222": {"detail": {"area": [{"city": "Dalian"}], "province": "Liaoning", "type": "domestic", "operator": "Telecom"} "location": "Liaoning Dalian Telecom"}, "responseHeader": {"status": 200," time ": 1539141421138," version ":" 1.1.0 "} * @ param tel * @ return * / @ RequestLine (" GET / open/location?tel= {tel} & qq-pf-to=pcqq.c2c ") JSONObject getDetailByPhone (@ Param (value =" tel ") String tel) } use
Can be called directly in the controller layer, directly on the code.
The most important place is the Feign.builder () this paragraph, the following is the judgment, because it is a third party, not their own interface, the judgment is more detailed, a little verbose.
/ * query region with mobile number * * @ param phone mobile number * @ return region * / private String getCityByPhone (String phone) {DetailPhoneClient detailPhoneClient = Feign.builder () .decoder (new GsonDecoder ()) .target (DetailPhoneClient.class, "http://mobsec-dianhua.baidu.com/dianhua_api");" JSONObject detailByPhone = detailPhoneClient.getDetailByPhone (phone); logger.info ("query the region where mobile phone number belongs:" + detailByPhone); / / get the data of mobile phone number information if (! detailByPhone.containsKey ("response")) {return null;} JSONObject response = detailByPhone.getJSONObject ("response"); if (! response.containsKey (phone)) {return null } JSONObject phoneDetail = response.getJSONObject (phone); if (! phoneDetail.containsKey ("detail")) {return null;} JSONObject detail = phoneDetail.getJSONObject ("detail"); if (! detail.containsKey ("area")) {return null;} JSONArray area = detail.getJSONArray ("area") If (area.size () = 0) {return null;} JSONObject cityJsonObject = area.getJSONObject (0); if (! cityJsonObject.containsKey ("city")) {return null;} return cityJsonObject.getString ("city");} in-depth understanding
If you can use it, let's learn more about it.
Authoritative book: github-openfeign (because this is easy to understand and authoritative, so the code does not copy, saving time and effort)
1. Gson/Jackson (processing data in json format) both include an encoder and a decoder
2. Sax/JAXB (dealing with data in xml format, because I haven't come into contact with this format yet. after all, json is more popular now, so we can only write here and use it.)
3. OkHttp,JAX-RS,Ribbon (load balancing) and Hystrix (circuit breaker mechanism). None of these are used. When they are used, check them on the official website.
4. You can add header to the interface. For details, you can check it on githua.
That's all for @ Headers ("Content-Type: application/xml") "how to use Feign to call a third-party http interface". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.