我將一組JSON對象從Angular1.x前端傳遞到Spring RestController後端。 該值在瀏覽器的HTTP請求負載中是正確的,但在Spring RestController中,其中一個屬性正在轉換爲零。JSON對象中的屬性值在Spring Rest Controller中轉換爲0
我無法調試原因。請幫幫我。
下面是在瀏覽器中的enter image description here快照請求負載的
[{ 「條款ArticleID 」:「 17070」, 「產品代碼」: 「1000」, 「產品名稱」: 「商業包裹區」, 「區」 :「1」},{「articleID」:「17071」,「productCode」:「1001」,「productName」:「商業包裹區域」,「區域」:「4」},{「articleID」:「17070」 ,「productCode」:「1012」,「productName」:「業務包裹區域」,「區域」:「5」},{「articleID」:「17070」,「productCode」:「1000」,「productName」商業包裹區域「,」區域「:」1「},{」articleID「:」17070「,」productCode「:」1000「,」productName「:」商業包裹區域「,」區域「:」2「}]
以下是我的控制器代碼:
@RestController
public class MyController {
@Autowired
MyService MyService;
@RequestMapping(value = "/savedata", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<ServiceArticle> saveData(@RequestBody List<ServiceArticle> serviceArticleList){
System.out.println("In savedata");
System.out.println(serviceArticleList.toString());
if(null != serviceArticleList && serviceArticleList.size() >0){
MyService.addAll(serviceArticleList);
}
// ResponseEntity<ServiceArticle> responseEntity = new ResponseEntity<ServiceArticle>(serviceArticle, HttpStatus.OK);
return null;
}
}
這是JSON對象陣列上調用的toString()的在Spring RestController接收的結果
[ServiceArticle [條款ArticleID = 0,PRODUCTCODE = 1000,產品名稱=商業包裹區,區= 1] ServiceArticle [articleID = 0,productCode = 1001,productName = Business Parcel zone,zone = 4],ServiceArticle [articleID = 0,productCode = 1012,productName = Business Parcel zone,zone = 5],ServiceArticle [articleID = 0,productCode = 1000,productName = Business Parcel zone,zone = 1],ServiceArticle [articleID = 0,productCode = 1000,productName = Business Parcel zone,zone = 2]]
articleID全部爲0 e數組中的ServiceArticle對象。這是我想弄清楚的問題。 articleID在請求負載中是正確的,但在RestController中轉換爲0。
下面是我的Java對象的結合,以JSON對象RequestBody
public class ServiceArticle {
private long articleID;
private long productCode;
private String productName;
private int zone;
public ServiceArticle(){
super();
}
public ServiceArticle(long articleID, long productCode, String productName, int zone) {
super();
this.articleID = articleID;
this.productCode = productCode;
this.productName = productName;
this.zone = zone;
}
public long getArticleID() {
return articleID;
}
public void setArticleID(long articleID) {
this.articleID = articleID;
}
public long getProductCode() {
return productCode;
}
public void setProductCode(long productCode) {
this.productCode = productCode;
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public int getZone() {
return zone;
}
public void setZone(int zone) {
this.zone = zone;
}
@Override
public String toString() {
return "ServiceArticle [articleID=" + articleID + ", productCode=" + productCode + ", productName="
+ productName + ", zone=" + zone + "]";
}
}
謝謝@BeginnersSake,它的工作。你是個天才。 – AkashSharma