2016-07-25 19 views
2

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); 
} 

ステータス郵便配達でそれをやっている間:

postman request

をReduxのが/取得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アノテーションを使用すると、オブジェクトを作成するときだけでなく、オブジェクトを出力するときにも使用できます。

私は要求を行うときにid propertyを無視している ので、あなたが今やるべきだと思うsetter method、上の使用@JsonIgnore注釈を。

@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); 
} 
関連する問題