I am using Java 15 preview feature record in my code, and defined the record as follow
public record ProductViewModel
(
String id,
String name,
String description,
float price
) {
}
In the controller level I have the below code
@Put(uri = "/{id}")
public Maybe<HttpResponse> Update(ProductViewModel model, String id) {
LOG.info(String.format("Controller --> Updating the specified product"));
return iProductManager.Update(id, model).flatMap(item -> {
if(item == null)
return Maybe.just(HttpResponse.notFound());
else
return Maybe.just(HttpResponse.accepted());
});
}
From the UI in the model the value of id is not passed, however, it is passed as a route parameter. Now I want to set the value in the controller level, something like
model.setid(id) // Old style
How can I set the value to the record particular property
You can't. Record properties are immutable. What you can do however is add a wither to create a new record with same properties but a new id: