feign调用分get和post最多,其他的put和delete不讲解
1、get请求
调用方传userId和size并绑定@RequestParam,服务提供方会自动解析到userId到User对象的UserId上,解析size到PageRequest 对象的size上。
服务提供方:
参数说明:
@PathVariable("organizationId") Long tenantId
服务提供方和服务调用方保持一致
User user
调用方无需传实体,传实体内的属性就可以比如userId
PageRequest pageRequest
调用方无需传实体,传实体内的属性就可以比如size
@ApiOperation("查询")
@GetMapping("/v1/{organizationId}/page")
public ResponseEntity<Page<User>> page(@PathVariable("organizationId") Long tenantId, User user, PageRequest pageRequest) {
Page<User> page = PageHelper.doPageAndSort(pageRequest, () -> service.page(user));
return Results.success(page);
}
服务调用方:
参数说明:
@PathVariable("organizationId") Long tenantId
服务提供方和服务调用方保持一致
@RequestParam("userId") Long userId
是服务提供方第二个参数User的一个属性必须用@RequestParam绑定属性名
@RequestParam("size") int size
是服务提供方第三个参数pageRequest的一个属性必须用@RequestParam绑定属性名
feign接口
@FeignClient(
value = "service",
fallbackFactory = RemoteServiceFallbackFactoryImpl.class,
path = "/v1/"
)
public interface RemoteService {
@GetMapping("{organizationId}/page")
ResponseEntity<Page<User>> page(@PathVariable("organizationId") Long tenantId,
@RequestParam("userId") Long userId,
@RequestParam("size") int size);
}
feign异常
@Slf4j
@Component
public class RemoteServiceFallbackFactoryImpl implements RemoteService{
@Override
public ResponseEntity<Page<User>> page(Long tenantId, Long userId, int size) {
log.info("获取数据失败,调用参数:{},{}", userId, size);
throw new CommonException("获取失败");
}
}
controller调用
@Slf4j
@RestController()
@RequestMapping({"/v1/{organizationId}"})
public class TestController extends BaseController {
@Autowired
PrmRemoteService remoteService;
@GetMapping(value = "/smy-test")
public ResponseEntity<?> smyTest() {
int size = 999;
ResponseEntity<Page<PrLine>> page= remoteService.page(0L, 1L, size);
return Results.success(page);
}
}
2、post请求
服务提供方:
参数说明:
@PathVariable("organizationId") Long tenantId
服务提供方和服务调用方保持一致
@RequestBody User user
服务提供方和服务调用方保持一致
PageRequest pageRequest
调用方无需传实体,传实体内的属性就可以比如size
@PostMapping({"/v1/{organizationId}/create"})
public ResponseEntity<Page<User>> create(@PathVariable("organizationId") Long tenantId,@RequestBody User user,PageRequest pageRequest) {
Page<User> page = lineService.create(tenantId, user, pageRequest);
return Results.success(page);
}
服务调用方:
feign接口:
@PathVariable("organizationId") Long tenantId
服务提供方和服务调用方保持一致
@RequestBody User user
服务提供方和服务调用方保持一致
PageRequest pageRequest
是服务提供方第三个参数pageRequest的一个属性必须用@RequestParam绑定属性名
@FeignClient(
value = "service",
fallbackFactory = RemoteServiceFallbackFactoryImpl.class,
path = "/v1/"
)
public interface RemoteService {
@PostMapping("/{organizationId}/create")
ResponseEntity<Page<PrLine>> create(@PathVariable("organizationId") Long tenantId,
@RequestBody User user,
@RequestParam("size") int size);
}
feign异常
@Slf4j
@Component
public class RemoteServiceFallbackFactoryImpl implements RemoteService{
@Override
public ResponseEntity<Page<User>> page(Long tenantId, User user, int size) {
log.info("数据失败,调用参数:{},{}", userId, size);
throw new CommonException("行失败");
}
}
controller调用
@Slf4j
@RestController()
@RequestMapping({"/v1/{organizationId}"})
public class TestController extends BaseController {
@Autowired
PrmRemoteService remoteService;
@GetMapping(value = "/smy-test1")
public ResponseEntity<?> smyTest1() {
int size = 999;
User user= new User();
user.setUserId(888L);
ResponseEntity<Page<PrLine>> page= prmRemoteService.create(21L,user,size);
return Results.success(page);
}
}
总结:
get请求调用方的形参必须都加注解,服务提供方是对象接收,调用方必须用@RequestParam修饰,多个就写多个。
post请求调用方必须有一个@RequestBody注解传对象且只能有一个,若有其他对象接收用@RequestParam修饰,多个就写多个。