Please correct me if I am wrong. Both can be used for Data Binding.
The question is when to use @ModelAttribute?
@RequestMapping(val
If you want to do file upload, you have to use @ModelAttribute
. With @RequestBody
, it's not possible. Sample code
@RestController
@RequestMapping(ProductController.BASE_URL)
public class ProductController {
public static final String BASE_URL = "/api/v1/products";
private ProductService productService;
public ProductController(ProductService productService) {
this.productService = productService;
}
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public ProductDTO createProduct(@Valid @ModelAttribute ProductInput productInput) {
return productService.createProduct(productInput);
}
}
ProductInput class
@Data
public class ProductInput {
@NotEmpty(message = "Please provide a name")
@Size(min = 2, max = 250, message = "Product name should be minimum 2 character and maximum 250 character")
private String name;
@NotEmpty(message = "Please provide a product description")
@Size(min = 2, max = 5000, message = "Product description should be minimum 2 character and maximum 5000 character")
private String details;
@Min(value = 0, message = "Price should not be negative")
private float price;
@Size(min = 1, max = 10, message = "Product should have minimum 1 image and maximum 10 images")
private Set<MultipartFile> images;
}