spring @RequestBody 正在获取空值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38935912/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:10:03  来源:igfitidea点击:

@RequestBody is getting null values

springspring-bootspring-restcontroller

提问by Geek

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

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

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;

    }


}

My java Bean object is like below:

我的 java Bean 对象如下所示:

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 is printing a null as a value for PolicyNumber.

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

Please help me to resolve this issue.

请帮我解决这个问题。

JSON which i am passing in request body is

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

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

I have even set Content-Typeto application/jsonin postman

我什Content-Type至设置为application/json邮递员

回答by AmanSinghal

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

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

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

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.

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

回答by Adriano Dib

Java convention demands the name of variable in a POJO (attribute of a class) must to be the first character in lowercase.

Java 约定要求 POJO(类的属性)中的变量名称必须是小写的第一个字符。

You have uppercase letters in your JSON properties, which is what is causing the failure.

您的 JSON 属性中有大写字母,这就是导致失败的原因。

回答by vedavyasa k

Check the @RequestBody import that will cause the problem.

检查将导致问题的@RequestBody 导入。

It should be --> import org.springframework.web.bind.annotation.RequestBody;

应该是 --> import org.springframework.web.bind.annotation.RequestBody;

回答by menakshisundaram

Setter would have been missed. So, Object values do not get set.

二传手会被错过。因此,对象值不会被设置。

回答by Jorge Moraleda

Use the annotation org.springframework.web.bind.annotation.RequestBodyand not org.springframework.web.bind.annotation.ResponseBody

使用注释org.springframework.web.bind.annotation.RequestBody而不是org.springframework.web.bind.annotation.ResponseBody