我使用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,在做的在邮递员:
我使用终极版/提取API时得到的错误(我只删除目录结构,因为它有公司名称):
已经停留了一段时间,任何帮助都非常感谢!
编辑资产对象:
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;
}
}
可以复制/粘贴您的资产Java对象? –
添加了代码。 – Matthias
你错过了你的json中的ID,因此得到了400.你能尝试告诉我结果吗? –