@RequestBody 正在获取空值
@RequestBody is getting null 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;
}
}
我的 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 正在打印空值作为 PolicyNumber 的值。
请帮我解决这个问题。
JSON 我在请求正文中传递的是
{
"PolicyNumber": "123",
"Type": "Test",
"Tenture": "10",
"SDate": "10-July-2016",
"HName": "Test User",
"Age": "10"
}
我什至在 postman
中将 Content-Type
设置为 application/json
尝试将 JSON 中属性的第一个字符设置为小写。例如
{
"policyNumber": "123",
"type": "Test",
"tenture": "10",
"sDate": "10-July-2016",
"hName": "Test User",
"age": "10"
}
基本上,Spring 使用 getter 和 setter 来设置 bean 对象的属性。它获取 JSON 对象的 属性,将其与同名的 setter 匹配。例如,要设置 policyNumber 属性,它会尝试在您的 bean class 中找到名称为 setpolicyNumber() 的 setter,并使用它来设置您的 bean 对象的值。
Java 约定要求 POJO 中的变量名(class 的属性)必须是小写的第一个字符。
您的 JSON 属性中有大写字母,这是导致失败的原因。
Setter 会被遗漏。因此,未设置对象值。
使用注释 org.springframework.web.bind.annotation.RequestBody
而不是 org.springframework.web.bind.annotation.ResponseBody
检查会导致问题的@RequestBody 导入。
应该是 --> import org.springframework.web.bind.annotation.RequestBody;
如果您无权更改 JSON 格式但仍想解决此问题,请尝试添加
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
DTO 之前的注释(示例中的策略)class.
我的 pom 中有 lombok,我的 bean 中有 lombok 注释。我还没有用我的 STS 正确安装 lombok,并且有类似的问题,我的 bean 没有被填充。
当我删除 lombok 注释时,我的 bean 被正确填充。
似乎是 lomboc 未正确安装在 STS 上 + lomboc 注释在我的 bean 上的组合。
我的情况是 Lombok 问题。我删除了所有的 lombok 注释并手动创建了构造函数 setter 和 getter。
作为建议,我还会将 JSON 设置为小写以遵循约定。
在我的例子中,必须定义空构造函数。
public MyClass(){}
如果您使用的是 Lombok Api,则没有 Getter 和 Setter 对 @[ 公开可见或可用=14=]ResponseBody 和@RequestBody 注解。
这就是为什么我们读取带有 null 值的 JSON 请求。
所以你需要注释那些@Getter、@Setter注解来接收JSON响应和读取JSON请求对象并生成相同的getters和setters。
重新启动或热加载(使用 Spring-Boot-Devtools)服务器,它应该可以正常工作。
您仍然可以将您的 lombok api 用于其他实体或 BO。
除了 lowerCamelCasing
之外,对我来说还需要为每个 getter 和 setter 方法应用 @JsonProperty(value="your expected JSON KEY name")
并在下面使用 this
运算符POJO/Bean/DTO class.
示例代码:
@JsonProperty(value="policyNumber")
public void setPolicyNumber(String policyNumber) {
this.policyNumber = policyNumber;
}
有同样的问题,但对于我的情况,只有一个字段没有设置。请求正文对象上的日志显示它被接收为空。删除了字段的 getters 和 setters 并使用 IDE 自动生成它们并且一切正常。
我高度怀疑 getter 和 setter 定义中的不匹配也可能导致此
我也遇到过这个问题,但我解决这个问题的最好方法是在我的 json 值
的每个字段初始化中检查第一个引号后的空格
参见spring PropertyNamingStrategy(UPPER_CAMEL_CASE,LOWER_CAMEL_CASE,LOWER_CASE
等等...默认 SNAKE_CASE)。
Spring 将通过命名策略自动更改 http contorller class 参数,这可能与您的请求不一致 json
以 SNAKE_CASE 作为 ex,当 java controller class 中的“myToken”时,您的客户端应该发送 my_token 而不是 myToken
我创建了一个简单的 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;
}
}
我的 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 正在打印空值作为 PolicyNumber 的值。
请帮我解决这个问题。
JSON 我在请求正文中传递的是
{
"PolicyNumber": "123",
"Type": "Test",
"Tenture": "10",
"SDate": "10-July-2016",
"HName": "Test User",
"Age": "10"
}
我什至在 postman
中将Content-Type
设置为 application/json
尝试将 JSON 中属性的第一个字符设置为小写。例如
{
"policyNumber": "123",
"type": "Test",
"tenture": "10",
"sDate": "10-July-2016",
"hName": "Test User",
"age": "10"
}
基本上,Spring 使用 getter 和 setter 来设置 bean 对象的属性。它获取 JSON 对象的 属性,将其与同名的 setter 匹配。例如,要设置 policyNumber 属性,它会尝试在您的 bean class 中找到名称为 setpolicyNumber() 的 setter,并使用它来设置您的 bean 对象的值。
Java 约定要求 POJO 中的变量名(class 的属性)必须是小写的第一个字符。
您的 JSON 属性中有大写字母,这是导致失败的原因。
Setter 会被遗漏。因此,未设置对象值。
使用注释 org.springframework.web.bind.annotation.RequestBody
而不是 org.springframework.web.bind.annotation.ResponseBody
检查会导致问题的@RequestBody 导入。
应该是 --> import org.springframework.web.bind.annotation.RequestBody;
如果您无权更改 JSON 格式但仍想解决此问题,请尝试添加
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
DTO 之前的注释(示例中的策略)class.
我的 pom 中有 lombok,我的 bean 中有 lombok 注释。我还没有用我的 STS 正确安装 lombok,并且有类似的问题,我的 bean 没有被填充。
当我删除 lombok 注释时,我的 bean 被正确填充。
似乎是 lomboc 未正确安装在 STS 上 + lomboc 注释在我的 bean 上的组合。
我的情况是 Lombok 问题。我删除了所有的 lombok 注释并手动创建了构造函数 setter 和 getter。
作为建议,我还会将 JSON 设置为小写以遵循约定。
在我的例子中,必须定义空构造函数。
public MyClass(){}
如果您使用的是 Lombok Api,则没有 Getter 和 Setter 对 @[ 公开可见或可用=14=]ResponseBody 和@RequestBody 注解。 这就是为什么我们读取带有 null 值的 JSON 请求。
所以你需要注释那些@Getter、@Setter注解来接收JSON响应和读取JSON请求对象并生成相同的getters和setters。 重新启动或热加载(使用 Spring-Boot-Devtools)服务器,它应该可以正常工作。 您仍然可以将您的 lombok api 用于其他实体或 BO。
除了 lowerCamelCasing
之外,对我来说还需要为每个 getter 和 setter 方法应用 @JsonProperty(value="your expected JSON KEY name")
并在下面使用 this
运算符POJO/Bean/DTO class.
示例代码:
@JsonProperty(value="policyNumber")
public void setPolicyNumber(String policyNumber) {
this.policyNumber = policyNumber;
}
有同样的问题,但对于我的情况,只有一个字段没有设置。请求正文对象上的日志显示它被接收为空。删除了字段的 getters 和 setters 并使用 IDE 自动生成它们并且一切正常。
我高度怀疑 getter 和 setter 定义中的不匹配也可能导致此
我也遇到过这个问题,但我解决这个问题的最好方法是在我的 json 值
的每个字段初始化中检查第一个引号后的空格参见spring PropertyNamingStrategy(UPPER_CAMEL_CASE,LOWER_CAMEL_CASE,LOWER_CASE 等等...默认 SNAKE_CASE)。 Spring 将通过命名策略自动更改 http contorller class 参数,这可能与您的请求不一致 json 以 SNAKE_CASE 作为 ex,当 java controller class 中的“myToken”时,您的客户端应该发送 my_token 而不是 myToken