温馨提示×

OpenFeign的Java接口定义规范是什么

小樊
108
2024-08-15 11:46:38
栏目: 编程语言

OpenFeign的Java接口定义规范如下:

  1. 使用@FeignClient注解标记接口,指定服务名称和路径
@FeignClient(name = "service-name", path = "/api")
public interface MyFeignClient {
    // 接口方法
}
  1. 定义接口方法,并使用@RequestMapping注解指定请求方法、路径和参数
@FeignClient(name = "service-name", path = "/api")
public interface MyFeignClient {
    
    @RequestMapping(method = RequestMethod.GET, value = "/users/{userId}")
    User getUserById(@PathVariable("userId") Long userId);
}
  1. 定义接口方法参数和返回值
public class User {
    private Long id;
    private String name;
    
    // getters and setters
}
  1. 使用@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);
}
  1. 接口方法可以定义任意类型的参数和返回值,包括基本类型、对象类型、集合类型等
@FeignClient(name = "service-name", path = "/api")
public interface MyFeignClient {
    
    @RequestMapping(method = RequestMethod.POST, value = "/users")
    void createUser(User user);
}

0