2

I have a 2 method: first one 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 one 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, I am surprized that, if I sent same data post method works fine(screen1), but put(screen2) method return validation error. screen1(post) enter image description here

screen2(put) enter image description here

What the 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 and setters
}
M. Deinum
  • 115,695
  • 22
  • 220
  • 224
N0D1R
  • 313
  • 2
  • 11

1 Answers1

0

I can see you have @Validated that should validate your request body according to JSR-303.

Seems like it is not consistent when you POST and PUT. It validates/not validating and return an error because your body does not match the validation rules you placed on ProductDTO.

In all the docs I saw you should do something like @Valid @RequestBody instead of just putting @Validated.

Try to change it to the above and see if it now work more consistently.

Tom
  • 3,711
  • 2
  • 25
  • 31
  • If I change `@Validated` to `@Valid @RequestBody`, server couldn't parse Multipart file. This error appers: `org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.web.multipart.MultipartException: Could not parse multipart servlet request; nested exception is java.io.IOException: org.apache.tomcat.util.http.fileupload.FileUploadException: the request was rejected because no multipart boundary was found` – N0D1R Sep 13 '17 at 05:03
  • Wow, Put method returns Error#415, Unsupported MediaType :-) – N0D1R Sep 13 '17 at 05:08
  • This is a different problem, look on this posts: [link](https://stackoverflow.com/questions/9081079/rest-http-post-multipart-with-json) and [link](https://stackoverflow.com/questions/9344455/unable-to-send-a-multipart-mixed-request-to-spring-mvc-based-rest-service) – Tom Sep 13 '17 at 14:14