@RequestBody 正在获取空值 [英] @RequestBody is getting null values

查看:25
本文介绍了@RequestBody 正在获取空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的 REST 服务 (POST).但是当我从邮递员那里调用这项服务时,@RequestBody 没有收到任何值.

I have created a simple REST service (POST). But when i call this service from postman @RequestBody is not receiving any values.

import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
public class Add_Policy {
    @ResponseBody
    @RequestMapping(value = "/Add_Policy", headers = {
            "content-type=application/json" }, consumes = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.POST)
    public Policy GetIPCountry( @RequestBody Policy policy) {

        System.out.println("Check value: " + policy.getPolicyNumber());
        return policy;

    }


}

我的 java Bean 对象如下所示:

My java Bean object is like below:

public class Policy {
    private String PolicyNumber;
    private String Type;
    private String Tenture;
    private String SDate;
    private String HName;
    private String Age;

    public String getPolicyNumber() {
        return PolicyNumber;
    }

    public void setPolicyNumber(String policyNumber) {
        PolicyNumber = policyNumber;
    }

    public String getType() {
        return Type;
    }

    public void setType(String type) {
        Type = type;
    }

    public String getTenture() {
        return Tenture;
    }

System.out.println 正在打印一个 null 作为 PolicyNumber 的值.

System.out.println is printing a null as a value for PolicyNumber.

请帮我解决这个问题.

我在请求正文中传递的 JSON 是

JSON which i am passing in request body is

{
    "PolicyNumber": "123",
    "Type": "Test",
    "Tenture": "10",
    "SDate": "10-July-2016",
    "HName": "Test User",
    "Age": "10"
}

我什至在 postman 中将 Content-Type 设置为 application/json

I have even set Content-Type to application/json in postman

推荐答案

尝试将 JSON 中属性的第一个字符设置为小写.例如.

Try setting the first character of the properties in your JSON to lower case. Eg.

{
    "policyNumber": "123",
    "type": "Test",
    "tenture": "10",
    "sDate": "10-July-2016",
    "hName": "Test User",
    "age": "10"
}

基本上,Spring 使用 getter 和 setter 来设置 bean 对象的属性.并且它采用 JSON 对象的属性,将它与同名的 setter 匹配.例如,要设置 policyNumber 属性,它会尝试在您的 bean 类中查找名为 setpolicyNumber() 的 setter,并使用它来设置 bean 对象的值.

Basically, Spring uses getter and setter to set the properties of the the bean object. And it takes the property of the JSON object, matches it with the setter of the same name. eg to set the policyNumber property it tries to find a setter with the name setpolicyNumber() in your bean class and use that to set the value of your bean object.

这篇关于@RequestBody 正在获取空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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