2016-07-25 51 views
2

我使用Java Spring Cloud/Boot構建了一個REST API服務。首先,我做了一個簡單的類連接到一個MongoDB和一個控制器,該控制器應該允許我添加,刪除,更新和獲取所有對象。當使用POSTMAN這些都可以工作,但是當我想使用redux和fetch API添加或更新一個對象時,我得到一個狀態400和「錯誤的請求」錯誤。這似乎與我在正文中發送的JSON有關,但它與例如POSTMAN一起使用的JSON格式完全相同。Java Spring REST API狀態400對POST/PUT的響應

我在Redux中的操作。爲了簡單/測試的目的,我在頂部添加了一個對象,而不是使用從頁面發送的對象。

var assetObject = { 
    "vendor" : "why dis no work?", 
    "name": "wtf", 
    "version": "231", 
    "category" : "qsd", 
    "technology" : "whatever" 
} 

export function addAsset(access_token, asset) { 
    return dispatch => { 
    fetch(constants.SERVER_ADDRESS + '/as/asset/add', 
     { 
     method: 'POST', 
     credentials: 'include', 
     headers: { 
      'Authorization': 'Bearer' + access_token, 
      'Content-Type': 'application/json' 
     }, 
     body: assetObject 
     }) 
     .then(res => dispatch({ 
     type: constants.ADD_ASSET, 
     asset 
     })) 
    } 
} 

控制器代碼的Java春:

@RequestMapping(method = RequestMethod.POST, path = "/add") 
public void addAsset(@RequestBody Asset asset) { 
    assetService.addAsset(asset); 
} 

狀態OK,在做的在郵遞員:

postman request

我使用終極版/提取API時得到的錯誤(我只刪除目錄結構,因爲它有公司名稱):

request error in browser

已經停留了一段時間,任何幫助都非常感謝!

編輯資產對象:

import org.springframework.data.annotation.Id; 
import org.springframework.data.mongodb.core.mapping.Document; 

@Document(collection = "assets") 
public class Asset { 

    @Id 
    private String id; 

    private String vendor; 
    private String name; 
    private String version; 
    private String category; 
    private String technology; 

    public Asset() { 
    } 

    public Asset(String id, 
       String vendor, 
       String name, 
       String version, 
       String category, 
       String technology) { 
     this.id = id; 
     this.vendor = vendor; 
     this.name = name; 
     this.version = version; 
     this.category = category; 
     this.technology = technology; 
    } 

    public String getId() { 
     return id; 
    } 

    public String getVendor() { 
     return vendor; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getVersion() { 
     return version; 
    } 

    public String getCategory() { 
     return category; 
    } 

    public String getTechnology() { 
     return technology; 
    } 

    public void setId(String id) { 
     this.id = id; 
    } 

    public void setVendor(String vendor) { 
     this.vendor = vendor; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

    public void setVersion(String version) { 
     this.version = version; 
    } 

    public void setCategory(String category) { 
     this.category = category; 
    } 

    public void setTechnology(String technology) { 
     this.technology = technology; 
    } 
} 
+0

可以複製/粘貼您的資產Java對象? –

+0

添加了代碼。 – Matthias

+0

你錯過了你的json中的ID,因此得到了400.你能嘗試告訴我結果嗎? –

回答

2

您的錯誤信息說:

;需要請求主體缺少

我認爲當你controller方法 試圖形成從傳入請求的對象發生錯誤。

當你發送請求時你必須要set每個字段與對象有關。 如果您打算未設置屬性,則應使用@JsonIgnore註釋標記該字段。

您可以在變量上使用@JsonIgnore註釋,在形成對象時以及在輸出對象時將忽略此屬性 。

使用@JsonIgnore註解放在setter method,我認爲你應該做的,因爲現在 發出請求時,你忽略了id property

@JsonIgnore 
public void setId(String id) { 
     this.id = id; 
} 

,你可以從controller method返回httpstatus代碼, 讓客戶知道請求成功

@ResponseBody 
public ResponseEntity<String> addAsset(@RequestBody Asset asset) { 
return new ResponseEntity<String>("your response here", HttpStatus.OK); 
}