单体项目使用openFeign调用远程接口

本文介绍了如何在SpringBoot2.7环境中使用OpenFeign进行远程服务调用,包括依赖引入、接口声明以及在实际控制器中的使用示例。
摘要由CSDN通过智能技术生成

背景

使用openFeign来进行远程调用很方便,他已经帮你封装了http请求的细节,只需要我们声明接口就好,这样的好处是:表达性更强,好维护一些。

环境

  • jdk:1.8
  • Springboot版本:2.7

使用

依赖引入:
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
            <version>3.1.9</version>
</dependency>
接口声明
package com.test.modules.xcx.client;

import io.swagger.annotations.ApiOperation;
import com.test.core.tool.api.R;
import com.test.modules.xcx.dto.BorrowerDTO;
import com.test.modules.xcx.vo.WechatMiniLawCasePackageVO;
import com.test.modules.xcx.vo.WechatMiniLawCaseVO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;


@FeignClient(
	name = "XXXClient",
	url = "远程地址"
)
public interface XXXClient {

	@PostMapping("/exist")
    R<Boolean> exist(@RequestBody XXXDTO xxxDTO);

}
使用
@RestController
@RequestMapping("/xxx")
public class XXXController {

	@Autowired
	private XXXClient client;

	@PostMapping("/exist")
	public R<Boolean> exist(@RequestBody XXXDTO xxxDTO) {
		return client.exist(xxxDTO);
	}

}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值