Let's say I want to build a rest api for storing car information. To make it simple for the sake of this post let's say I would like it to look like this:
/api/cars/{carmake}/save
/api/cars/{carmake}/edit
/api/cars/{carmake}/delete
Now, let's say I have multiple car makes and each of them would require different car make services eg. BmwService, MercedesService, AudiService.
So this is my idea: one abstract controller that would look something like this:
@RequestMapping(value="/api/cars/")
public abstract class CarController {
protected final String CAR_MAKE;
public CarController(String carMake){
this.CAR_MAKE = carMake;
}
@PostMapping(value = CAR_MAKE + "/save")
public abstract void save(@Valid @RequestBody Serializable car)
@DeleteMapping(value = CAR_MAKE + "/delete")
public abstract void save(@Valid @RequestBody Serializable car);
@PatchMapping(value = CAR_MAKE + "/edit")
public abstract void save(@Valid @RequestBody Serializable car)
}
And then an actual controller could look something like this:
@RestController
public class AudiController extends CarController {
private AudiService audiService;
@Autowired
public AudiController(AudiService audiService){
super("audi");
this.audiService = audiService;
}
@Override
public void save(@Valid @RequestBody Serializable car) {
audiService.save((Audi) car);
}
.
.
.
}
The problem is that spring does not allow me to have the value for request mappings with a final variable if it is initialized through the constructor (if the CAR_MAKE is initialized right on the field declaration eg. protected final String CAR_MAKE = "s"
it works). So is there any way to work around this so that the paths can come from each subclass?