OpenFeign的Java接口定義規范如下:
@FeignClient
注解標記接口,指定服務名稱和路徑@FeignClient(name = "service-name", path = "/api")
public interface MyFeignClient {
// 接口方法
}
@RequestMapping
注解指定請求方法、路徑和參數@FeignClient(name = "service-name", path = "/api")
public interface MyFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "/users/{userId}")
User getUserById(@PathVariable("userId") Long userId);
}
public class User {
private Long id;
private String name;
// getters and setters
}
@RequestParam
、@PathVariable
等注解指定參數來源和值@FeignClient(name = "service-name", path = "/api")
public interface MyFeignClient {
@RequestMapping(method = RequestMethod.GET, value = "/users")
List<User> getUsers(@RequestParam("page") int page, @RequestParam("size") int size);
}
@FeignClient(name = "service-name", path = "/api")
public interface MyFeignClient {
@RequestMapping(method = RequestMethod.POST, value = "/users")
void createUser(User user);
}