阅读量:0
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); }