OpenFeign的Java接口定义规范是什么
VPS评测- 服务器
- 2025-06-01 23:03:57
- 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;
}
- 使用@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);
}