本文主要是介绍单体项目使用openFeign调用远程接口,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
背景
使用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 {@Autowiredprivate XXXClient client;@PostMapping("/exist")public R<Boolean> exist(@RequestBody XXXDTO xxxDTO) {return client.exist(xxxDTO);}}
这篇关于单体项目使用openFeign调用远程接口的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!