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 use Feign to invoke declarative service in Spring MVC

2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

本篇文章为大家展示了Spring MVC中怎么使用Feign调用声明式服务,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

为什么要使用声明式服务调用?

对系统使用方,通过设计声明式的接口,开发者无需关心底层实现,而更多的关注上层业务

对系统实现方,通过声明式的接口,上层使用者接口相对稳定前提下,系统可以不断的迭代优化

对整个系统而言,能够更系统的收集更多信息,能够依据策略进行系统行为优化,提升系统效率

Feign声明式web客户端

使用Feign,只需要声明一个接口即可,不需要关心传参、发送请求、获取响应内容、关闭连接等细节,Feign全部帮我们做好了。

SpringCloud集成了Feign组件,使得SpringCloud服务间调用变得更简单,方便

这里并不是SpringCloud的项目,那如何引入Feign到普通的SpringMVC项目中呢?

SpringMVC集成Feign客户端1.引入maven依赖 io.github.openfeign feign-core 10.2.0 io.github.openfeign feign-gson 10.2.02. 创建接口,声明接口方法

这里使用YesAPI作为第三方服务调用测试

这里以全国大学接口为例:

可以根据大学名称、学校类型、所在省份、所在城市等搜索大学

请求(查找全部师范大学):http://api.yesapi.cn/?s=App.Common_University.Search&school_name=师范&app_key={你的app_key}&sign={动态签名}返回:{ "ret": 200, "data": { "err_code": 0, "err_msg": "", "schools": [ { "school_name": "北京师范大学", "school_province": "北京", "school_level": "本科", "school_website": "http://www.bnu.edu.cn/", "school_city": "北京市" }... ] }, "msg": "当前请求接口:App.Common_University.Search"}1.封装返回实体

可以看到小白开放平台是有统一返回体的,我们可以封装起来,也可以直接用Object或者Map来接收数据.我选择数据封装.

YesResponse.java

@Datapublic class YesResponse { private Integer ret; private String msg; private T data;}

YesUniversity.java

@Datapublic class YesUniversity { private String err_code; private String err_msg; private List schools;}

School.java

@Datapublic class School { private String school_name; private String school_province; private String school_level; private String school_website; private String school_city;}2.声明参数

既然是声明式服务调用,必须先声明再调用,结果已经声明了,接下来就是声明参数了,我依然选择数据封装;

可以从上面的请求示例看到,需要3个参数.

YesVo.java

@Datapublic class YesVo { private String school_name; private String app_key; private String sign;}3.声明接口

参数和结果都已经封装好了,接下来就是声明服务接口了

一般是根据对方的uri命名接口

Yes.java

public interface Yes { @RequestLine("POST /?s=App.Common_University.Search") YesResponse appCommonUniversitySearch(@QueryMap YesVo vo);}

如上,一个服务接口已经声明好了,因为这里使用的是post请求,@QueryMap可以把对象转为body体的参数,@RequestLine可以声明其服务路径

4.服务接口调用

通过service层的封装,可以把一些业务逻辑写在里面

public class YesService { public YesResponse appCommonUniversitySearch(){ Yes yes=Feign.builder().decoder(new GsonDecoder()).target(Yes.class,"http://api.yesapi.cn"); YesVo yesVo=new YesVo(); yesVo.setSchool_name("师范"); yesVo.setApp_key("你的app_key"); yesVo.setSign("你的sign"); return yes.appCommonUniversitySearch(yesVo); } public static void main(String[] args) { YesService yesService = new YesService(); YesResponse yesUniversityYesResponse = yesService.appCommonUniversitySearch(); System.out.println(JSON.toJSONString(yesUniversityYesResponse)); }}

那出现400,500这些异常怎么办?

Feign组件考虑到了,Feign封装了一个Exception叫FeignException

结构如下图:这样我们可以通过这个FeignException的内置API达到我们对接服务的效果.

If it is a service of the same platform, you can directly add an interface method to the corresponding interface: for example, Yes interface

@RequestLine("GET ? service={service}&app_key={appKey}&sign={sign}")Result res(@Param("service") String service,@Param("appKey") String appKey, @Param("sign") String sign); The above is how to use Feign to invoke declarative services in Spring MVC. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserves, please pay attention to 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