简体   繁体   English

Spring Boot结构:是否在控制器或服务层中验证图像大小?

[英]Spring Boot Structure: Validation of image size in controller or service layer?

I only want to allow images of 600 x 600 pixels for a product. 我只想允许某产品的600 x 600像素的图像。

Should I include this validation rule in the controller method or is this validation part of the service layer (with exception throwing)? 我应该在控制器方法中包括此验证规则,还是该验证部分在服务层中(引发异常)?

@PostMapping("{id}/edit/uploadProductImage")
public String uploadProductImage(...) throws IOException {

  // ...

  BufferedImage image = ImageIO.read(imageForm.getFile().getInputStream());
  Integer width = image.getWidth();
  Integer height = image.getHeight();

  if (!width.equals(600) && !height.equals(600)) {
      model.addAttribute("imageErrorMessage", "Wrong size!");
      result.reject("file");
  } 

  // >>> or put validition to service layer and throw exception?
  productService.storeTeaserImageForProduct(product.get(), imageForm.getFile());

  //...

}

Can you give me a hint? 你能给我一个提示吗?

I would put image size validation as a method in the service layer. 我会将图像大小验证作为服务层中的一种方法。 You might have to do more image size validation in the future - then you can't reuse your code in the controller. 将来您可能需要做更多的图像大小验证-然后就无法在控制器中重用代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM