Reputation: 37058
I have controller:
@RestController
public class AdminController {
@PutMapping("/path/max_file_size")
public void setMaxFileSize(@ModelAttribute MaxFileSizeDto size) {
System.out.println(size.getSize());
}
public static class MaxFileSizeDto {
private long size;
public long getSize() {
return size;
}
public void setSize(long size) {
this.size = size;
}
}
}
I send htpp request with postman:
but I always gets 0 for
size.getSize()
what do I wrong?
Upvotes: 2
Views: 774
Reputation: 3917
Before Adding model you should remember that, Model
and Controller
should be segregated. That means keep your model in another package or class.For posting/putting data you should use Object
(String, Integer,Long) instead of primitive
(int, long etc).
Suppose your Model Class
MaxFileSizeDto.java
public static class MaxFileSizeDto {
private Long size;
public Long getSize() {
return size;
}
public void setSize(Long size) {
this.size = size;
}
}
And your controller class will be:
@RestController
public class AdminController {
@PutMapping("/path/max_file_size")
public void setMaxFileSize(@ModelAttribute MaxFileSizeDto size) {
System.out.println(size.getSize());
}
}
When you sent data as application/x-www-form-urlencoded
then your data will be sent in this format and directly bind in @ModelAttribute
defined class.
param1=data1¶m2=data2¶m3=data3
However postman internally send the data in this format.
If you want to send data as a @RequestBody
then you should use json data saying that your content type is application/json
. To do that you should select raw
radio button from postman and select application/json
from drop down.(last element in the row)
In these case data will be
{
"size":123456
}
Upvotes: 1