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);
}