为什么我的休息端点收到空的 dto [英] Why my rest endpoint receives empty dto

查看:73
本文介绍了为什么我的休息端点收到空的 dto的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RestTemplate 将 pojo 发布到休息端点

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);

但我在服务器端收到空的 dto

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;
}

Dto 是简单的 pojo

The Dto is simple pojo

public class Dto {
    private String phone;

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

推荐答案

尝试将 @RequestBody 注解添加到您的 test123 方法中:

Try adding the @RequestBody annotation to your test123 method:

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

这篇关于为什么我的休息端点收到空的 dto的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆