mardi 11 août 2020

RESTful Web Service Client - Pass Objects or only id of objects

I have an web service application with an endpoint (/car) that returns a list of available cars stored in a DB. Then the same application has another endpoint that returns the value of a car on a given date.

When requesting the value of cars, should the client pass in a list of car ids or a list of car objects? For example, using Spring REST controllers:

@RestController
@RequiredArgsConstructor
@RequestMapping("/car-value")
public class CarValueController {
    
    private final CarRepository carRepo;
    private final SportsCarClient sportsCarClient;
    private final StandardCarClient standardCarClient;

    @GetMapping
    public Map<Long, BigDecimal> getTotalReturn(Set<Long> carIds, LocalDate valuationDate) {
       
       List<Car> cars = carRepo.findAllById(carIds);
       BigDecimal value;
       for (Car car: cars) {
           if (car.getType().equals(CarType.SPORTS_CAR)) {
              //get from value from sports car service
              value = sportsCarClient.getValue(car.getVin());
           } else {
              value = standardCarClient.getValue(car.getStandardId());
           }
           // put value in map
       }
       //return map
}

or

@RestController
@RequiredArgsConstructor
@RequestMapping("/car-value")
public class CarValueController {
    
    private final SportsCarClient sportsCarClient;
    private final StandardCarClient standardCarClient;

    @PostMapping
    public Map<Long, BigDecimal> getTotalReturn(@RequestBody Set<Car> cars, LocalDate valuationDate) {
       
       BigDecimal value;
       for (Car car: cars) {
           if (car.getType().equals(CarType.SPORTS_CAR)) {
              //get from value from sports car service
              value = sportsCarClient.getValue(car.getVin());
           } else {
              value = standardCarClient.getValue(car.getStandardId());
           }
           // put value in map
       }
       //return map
}

This is question is similar to this post. I believe a few relevant differences are:

  1. The client is another web service although located on the same physical machine and also maintained by the same developers.
  2. The application with /car and /car-value endpoints connects to a DB that contains the car info (i.e. Car is an entity).
  3. Some clients don't need the car type, VIN, or standard id. So, they would normally not store it when getting the car data from /car. If they passed Car objects to /car-value, they would have to start storing these values. They would need to know that these values are used to get the car value.

Aucun commentaire:

Enregistrer un commentaire