java springboot 引用openfeign 接口轉發

1、build.gradle中引入組件

compile ("org.springframework.cloud:spring-cloud-starter-config:2.1.1.RELEASE")

compile ("org.springframework.cloud:spring-cloud-starter-openfeign:2.1.3.RELEASE")

2、Application啓動類中添加註解@EnableFeignClients

3、添加 接口

url 在配置項中,也可以直接寫死例如: http://hhh.com

@Service

@FeignClient(value = "TestApiService", url = "${test.serverUrl}")

public interface TestApiService {

/**

  • PostMapping中的value就是要轉發的地址

  • @param param

  • @return

    */

@PostMapping(value = "/api/v1/user/role")

CommonResponse<Object> userRole(@RequestBody UserRoleParam param);

}

4、在Controller中調用即可

@Autowired

private TestApiService testApiService;

/**

  • @return

    */

@PostMapping(value = "/v1/user/role/")

public CommonResponse<Object> userRole(@RequestBody UserRoleParam param) {

return testApiService.userRole(param);

}

這樣可以實現接口轉發

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章