Gee*_*eek 19 spring spring-boot spring-restcontroller
我创建了一个简单的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;
}
}
Run Code Online (Sandbox Code Playgroud)
我的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;
}
Run Code Online (Sandbox Code Playgroud)
System.out.println正在打印null作为PolicyNumber的值.
请帮我解决这个问题.
我在请求正文中传递的JSON是
{
"PolicyNumber": "123",
"Type": "Test",
"Tenture": "10",
"SDate": "10-July-2016",
"HName": "Test User",
"Age": "10"
}
Run Code Online (Sandbox Code Playgroud)
我甚Content-Type至打算application/json在邮递员
小智 32
检查将导致问题的@RequestBody 导入。
应该是 --> import org.springframework.web.bind.annotation.RequestBody;
Ama*_*hal 29
尝试将JSON中属性的第一个字符设置为小写.例如.
{
"policyNumber": "123",
"type": "Test",
"tenture": "10",
"sDate": "10-July-2016",
"hName": "Test User",
"age": "10"
}
Run Code Online (Sandbox Code Playgroud)
基本上,Spring使用getter和setter来设置bean对象的属性.它采用JSON对象的属性,将其与同名的setter匹配.例如,要设置policyNumber属性,它会尝试在bean类中找到名为setpolicyNumber()的setter,并使用它来设置bean对象的值.
小智 8
如果您无权更改 JSON 格式但仍想解决此问题,请尝试
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class)
在 DTO(示例中的策略)类之前添加注释。
| 归档时间: |
|
| 查看次数: |
28716 次 |
| 最近记录: |