Spring Rest Controller

时间:2017-02-11 22:49:40

标签: java json spring

我将一个JSON对象数组从Angular1.x前端传递给Spring RestController后端。 该值在浏览器的HTTP请求有效负载中是正确的,但在Spring RestController中,其中一个属性转换为零。

我无法调试原因。请帮帮我。

以下是浏览器中请求有效负载的enter image description here快照

[{“articleID”:“17070”,“productCode”:“1000”,“productName”:“Business Parcel zone”,“zone”:“1”},{“articleID”:“17071”,“ productCode“:”1001“,”productName“:”Business Parcel zone“,”zone“:”4“},{”articleID“:”17070“,”productCode“:”1012“,”productName“:”Business Parcel zone“,”zone“:”5“},{”articleID“:”17070“,”productCode“:”1000“,”productName“:”Business Parcel zone“,”zone“:”1“},{” articleID“:”17070“,”productCode“:”1000“,”productName“:”Business Parcel zone“,”zone“:”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;
}   

}

这是在Spring RestController中接收到的JSON对象数组上调用toString()的结果

[ServiceArticle [articleID = 0,productCode = 1000,productName = Business Parcel zone,zone = 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在数组中的所有ServiceArticle对象中都为0。这是我想弄清楚的问题。 articleID在请求有效负载中是正确的,但在RestController中转换为0。

下面是我的Java Object的代码,它绑定到RequestBody中的JSON对象

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 + "]";
}

}

1 个答案:

答案 0 :(得分:0)

检查您的输入JSON。您已在"articleID "中提供了空格。将"articleID "更改为"articleID"