rest - Spring Request Mapping post vs put, same method, same logic, but -
i have 2 method: first 1 create product:
@requestmapping(method = requestmethod.post) public responseentity<?> create(@validated productdto productdto){ productservice.addproduct(productdto); return new responseentity<>("maxsulot ro'yhatga qo'shildi", httpstatus.ok); }
another 1 update product:
@requestmapping(method = requestmethod.put) public responseentity<?> update(@validated productdto productdto){ productservice.update(productdto); return new responseentity<>("maxsulot ma'lumotlari yangilandi", httpstatus.ok); }
now, surprized that, if sent same data post method works fine(screen1), put(screen2) method return validation error. screen1(post)
what problem is? mydto class:
public class productdto { private long id; private multipartfile file; @notnull @size(min = 2, max = 50) private string productname; @notnull private long productprice; private string productinfo; @notnull private long categoryid; private string unitofmeasurement; // getters , setters }
i can see have @validated
should validate request body according jsr-303
.
seems not consistent when post
, put
. validates/not validating , return error because body not match validation rules placed on productdto
.
in docs saw should @valid @requestbody
instead of putting @validated
.
try change above , see if work more consistently.
Comments
Post a Comment