背景
使用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);
}
}