Why my rest endpoint receives empty dto

320 Views Asked by At

I am trying to post pojo to rest endpoint with RestTemplate

Dto dto = new Dto();
dto.setPhone("12313");
RestTemplate restTemplate = new RestTemplate();
restTemplate.postForObject(new URI("http://localhost:8080/test"), dto, Dto.class);

but I receive empty dto on the server side

@RequestMapping(value = "/test")
@ResponseBody
public DTO test123(DTO dto) {
    System.out.println(dto.getPhone()); // empty
    return dto;
}

The Dto is simple pojo

public class Dto {
    private String phone;

    public String getPhone() {
        return phone;
    }
    public void setPhone(String phone) {
        this.phone = phone;
    }
}
1

There are 1 best solutions below

0
On BEST ANSWER

Try adding the @RequestBody annotation to your test123 method:

public DTO test123(@RequestBody DTO dto) {
    System.out.println(dto.getPhone()); // empty
    return dto;
}