Why my rest endpoint receives empty dto

烈酒焚心 提交于 2020-01-16 04:54:06

问题


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:


Try adding the @RequestBody annotation to your test123 method:

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


来源:https://stackoverflow.com/questions/27128734/why-my-rest-endpoint-receives-empty-dto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!