繁体   English   中英

请求有效负载内的字段在Spring Boot资源中显示为null

[英]Fields within request payload are showing up as null in spring boot resource

我有以下类别,将其用作请求有效负载:

public class SampleRequest {

    private String fromDate;
    private String toDate;

    // Getters and setters removed for brevity.
}

我正在尝试使用下面的资源(只是尝试将其打印到屏幕上以查看发生的事情):

@PostMapping("/getBySignatureOne")
public ResponseEntity<?> getRequestInfo(@Valid @RequestBody SampleRequest sampleRequest) {
    System.out.println(sampleRequest.getToDate());
    System.out.println(sampleRequest.getFromDate());
}

这是我发送的JSON请求:

{
    "fromDate":"2019-03-09",
    "toDate":"2019-03-10"
}

println的输出是:

null
null

我需要做些什么来连接吗?

请在以下配置中找到相同的代码

Spring-Boot版本:2.1.3

调节器

package com.firsttest.controller;

import javax.validation.Valid;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.firsttest.pojo.SampleRequest;

@RestController
@RequestMapping("/test")
public class FirstTestController {



    @PostMapping("/getBySignatureOne")
    public ResponseEntity<?> getTestString(@Valid @RequestBody SampleRequest sampleRequest) {
        System.out.println("getFromDate : " + sampleRequest.getFromDate());
        System.out.println("getToDate : " + sampleRequest.getToDate());
        return null;//I dont want to send any response.
    }
}

SampleRequest.java

package com.firsttest.pojo;

public class SampleRequest {

    private String fromDate;
    private String toDate;

    public String getFromDate() {
        return fromDate;
    }
    public void setFromDate(String fromDate) {
        this.fromDate = fromDate;
    }
    public String getToDate() {
        return toDate;
    }
    public void setToDate(String toDate) {
        this.toDate = toDate;
    }

}

请求

http://localhost:8080/first/test/getBySignatureOne
httpmethod: POST
Content-Type:application/json
{
    "fromDate":"2019-03-09",
    "toDate":"2019-03-10"
}

输出是

getFromDate : 2019-03-09
getToDate : 2019-03-10

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM