Spring内容,如何在其他页面抓取图片

Spring content, how to fetch images at other pages

我对 Spring 内容有疑问。 我正在建立汽车租赁服务。用户有一个个人资料页面,其中包含他拥有的汽车以及有关它们的一些信息,包括图像。然后他可以添加一辆车,并且应该提供它的图像。我决定对我的 HSQLDB 数据库使用 Spring Content JPA 策略。 因此,我可以通过转到 link /data/{id} 来访问汽车图像。但我无法弄清楚如何在某些页面上获取该汽车图像。我应该向我的 Car 实体添加一个字段图像,还是 Spring 内容中有一个现有的解决方案?此外,该车将在其他用户的汽车中显示在租赁页面上。

汽车内容字段:

@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";
    }
}

看来您的方向是正确的。

您已将 @ContentId 在您的实体上定义为 String 类型,但在您的 ContentStore 上定义为 UUID 类型。这些应该是相同的,并且在使用 Spring 时,内容 JPA 的类型应该是 String.

假设您正在使用 Spring Content REST 并且它已启用(即您依赖 spring-content-rest-boot-starter 或导入 org.springframework.content.rest.RestConfiguration)那么此时您需要做的就是在您的 HTML:

中包含一个常规图像标签

<img src="/data/{id}"/>

当请求图像时,浏览器将发送一个 image-based Accept header,这应该使请求与提供内容的 StoreRestController.getContent 处理程序方法相匹配。应注意避免应用程序中的其他处理程序意外获取这些请求。