Java Spring Cloud/Bootを使用してREST APIサービスを構築しました。まず、MongoDBに接続されたシンプルなクラスと、すべてのオブジェクトを追加、削除、更新、取得できるサービスを備えたコントローラを作成しました。 POSTMANを使用する場合、これらはすべて動作しますが、reduxとfetch APIを使用してオブジェクトを追加または更新する場合は、ステータス400と「不正なリクエスト」エラーが発生します。これは私がボディに送信しているJSONに関係しているようですが、POSTMANと連携しているJSONとまったく同じ形式です。POST/PUTでのJava Spring REST APIステータス400の応答
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の春コントローラーコード:OK
@RequestMapping(method = RequestMethod.POST, path = "/add")
public void addAsset(@RequestBody Asset asset) {
assetService.addAsset(asset);
}
ステータス郵便配達でそれをやっている間:
をReduxのが/取得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が得られます。結果を教えてください。 –