我创建了一个简单的REST服务(POST)。但是当我从邮递员调用此服务@RequestBody没有收到任何值。@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"
}
我甚至在邮递员设置Content-Type
到application/json
将'@ ResponseBody'应用于方法的输出而不是方法本身。如果您期待JSON值,还需要包含“产品”标头值。 – 11thdimension
即使我将响应作为无效,请求中的值也是相同的null – Geek
'policy'本身不为null,您确定它包含'policyNumber'吗? – 11thdimension