查看“OpenFeign 无参接口调用”的源代码
←
OpenFeign 无参接口调用
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
https://www.bilibili.com/video/BV1My4y1W7vy/?p=3 === pom.xml === <syntaxhighlight lang="xml"> <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>io.github.jihch</groupId> <artifactId>feign-demo</artifactId> <version>1.0-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.4.RELEASE</version> </parent> <properties> <maven.compiler.source>8</maven.compiler.source> <maven.compiler.target>8</maven.compiler.target> <spring-cloud.version>Hoxton.SR8</spring-cloud.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-openfeign</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> </project> </syntaxhighlight> === application.yml === <syntaxhighlight lang="yaml"> spring: application: name: feign-demo server: port: 8088 eureka: client: service-url: defaultZone: http://localhost:8761/eureka/ </syntaxhighlight> === ServiceDemoFeign.java === <syntaxhighlight lang="java"> import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.RequestMapping; @FeignClient("MYPROJECT") public interface ServiceDemoFeign { /** * 方法要求: * 返回值:要对应 * 方法名:随意 * 参数:要对应 * 方法上添加 SpringMVC 注解 */ @RequestMapping("/demo") String suiyi(); } </syntaxhighlight>FeignClient 括号里的参数值就是要调用的目标接口在 Eureka 中注册的 application name [[文件:Feign 通过 application name 进行接口调用.png|无|缩略图|1198x1198像素]] === 将 FeignClient 注解的类注入到要进行调用的类 === ==== FeignDemoServiceImpl.java ==== <syntaxhighlight lang="java"> import io.github.jihch.feign.ServiceDemoFeign; import io.github.jihch.service.FeignDemoService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class FeignDemoServiceImpl implements FeignDemoService { @Autowired private ServiceDemoFeign serviceDemoFeign; @Override public String demo() { return serviceDemoFeign.suiyi(); } } </syntaxhighlight>注意:Feign 在应用程序中默认是不开启的,<code>private ServiceDemoFeign serviceDemoFeign;</code> 这行代码会编译报错,此时需要在启动类上加注解 <code>@EnableFeignClients</code> 来开启 Feign ==== FeignDemoApplication.java ==== <syntaxhighlight lang="java"> import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.openfeign.EnableFeignClients; @SpringBootApplication @EnableFeignClients public class FeignDemoApplication { public static void main(String[] args) { SpringApplication.run(FeignDemoApplication.class, args); } } </syntaxhighlight>访问 http://localhost:8088/demo 进行测试
返回至
OpenFeign 无参接口调用
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息