Arsen Nykytenko
Arsen Nykytenko

Reputation: 97

Spring binding PUT request params to an object

I have an endpoint for updating some Order details. This endpoint receives a DTO with updates.

@PutMapping
public void updateOrder(OrderUpdateDTO updates)

The problem is when I use @PostMapping everything works fine and my request successfully binds to an object. But when I use @PutMapping all my fields are nulls. When I looked into request object itself - it doesn't have any parameters at all.

Possible solution could be to mark a method argument with @RequestBody but the issue is that one of the fields in updates is a file so I submit the whole form as a FormData object (which I cannot access with @RequestBody).

I couldn't find any mentions in Spring docs about the fact why POST automatically binds requests to classes and PUT does not.

So I have 2 questions:

  1. How this Spring magic works (and why it works differently for POST and PUT)? (Or some links to any sources);
  2. How can I combine Spring @PutMapping with multipart request to upload a file?

Upvotes: 0

Views: 614

Answers (1)

memoricab
memoricab

Reputation: 453

Did you try to use separate Multipartfile from RequestBody and use RequestParam ?

@PutMapping
public Foo updateOrder(
    @RequestPart UpdateOrderDto updates,
    @RequestPart MultipartFile file)

In the above case, you need to put 2 fields into form, updates and file.

Upvotes: 0

Related Questions