I have a problem with Spring Content. I'm building a car rental service. The user has a profile page with the cars he owns and some information about them including an image. Then he can add a car and should provide an image of it. I decided to use Spring Content JPA strategy with my HSQLDB database. So, I can access a car image by going to the link /data/{id}. But I can't figure out how to fetch that car image at some page. Should I add a field image to my Car entity or there is an existing solution in Spring Content? Also, the car will be shown on the rental page among the cars of other users.
Car content fields:
@ContentId
private String contentId;
@ContentLength
private Long contentLength = 0L;
// if you have rest endpoints
@MimeType
private String mimeType = "image/png";
CarImageStore:
@StoreRestResource(path = "data")
@Repository
public interface CarImageStore extends ContentStore<Car, UUID> {
}
CarUIController:
@Controller
@RequestMapping("/cars")
public class CarUIController {
private final CarService service;
private final CarImageStore store;
public CarUIController(CarService service, CarImageStore store) {
this.service = service;
this.store = store;
}
@GetMapping
public String getAll(Model model) {
model.addAttribute("cars", service.getAll());
return "cars";
}
@PostMapping
public String create(Car car,
@RequestParam("file") MultipartFile file,
@AuthenticationPrincipal User authUser) {
store.setContent(car, file.getResource());
service.create(car, authUser.getId());
return "redirect:/profile";
}
}