我創建了一個簡單的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