Pretty simple situation.
My model is 1 Product has many Bookings
the api that I have to expose is just for bookings, the product part is just description and some price that has not to be edited.
so my desired endpoints are:
GET - List of all bookings POST - Create new booking PUT - Update existing Booking
The booking contains product reference but I don't want the endpoint to be "related" with the products so I keep the mapping like this:
@RequestMapping(value = "/bookings", method= RequestMethod.GET) public List<Booking> bookingsList(){ ...
My question is about the Create endpoint. Right now it's like this
@RequestMapping(value = "/{productId}/bookings",method= RequestMethod.POST) public String create(@PathVariable String productId,@RequestBody Booking input){ ...
As you can see I had to use a PathVariable to pass the productId
I was wondering if there is a way to keep the endpoint just /bookings and still pass the productId without create a specific "request model" just for deserialization. From my RoR experience params were generic and can contains lot's of fields not properly related to a specific model(or in this case is only the ID not the object)
product_id
doesn't match anything in yourBooking
object. I wouldn't map an entity directly in the view model as it ties your public API with your data model. So, yes for aBookingRequest
of some kind that match your public (json) API.There might be a way to map
product_id
to yourproduct#id
method, but:a) I am guessing you don't have any setter for that field since it's something that's not supposed to change
b) you expect a full
Product
instance to be available when you dobooking.getRequest()
. It won't be and I think that's something you shouldn't have to remember.The separate entity may looks like boilerplate at first but it's a critical aspect if you want to build a stable API for your endpoint.