为什么我的休息端点接收空的DTO(Why my rest endpoint receives emp

2019-10-21 08:05发布

我尝试后POJO休息端点RestTemplate

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

但我收到在服务器端空DTO

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

DTO的是简单的POJO

public class Dto {
    private String phone;

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

Answer 1:

尝试添加@RequestBody注释您test123方法:

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


文章来源: Why my rest endpoint receives empty dto