2017-10-02 9 views
0

Java MavenのSpringブートSpringMVCでREST APIを使用しています。 AngularのPOST要求パラメータは、SpringMVC @RequestParamによって認識されません。ここに角度コードがあります。Java SpringMVCへの角度4のHTTPポスト。 POSTパラメータが表示されません

saveAsSiteProduct(id: number, data: saveAsSiteProductSettings): Observable<any> { 
    const options = new RequestOptions({ headers: new Headers({ 'Content-Type': 'application/json' }) }); 
    return this.http 
     .post(this.amazonUrl + '/products/imported/' + id +'/saveas/siteproduct', JSON.stringify(data), options) 
     .map(response => response.json()); 
    } 

ここにJava SpringMVCコードがあります。

@PostMapping(value = "/products/imported/{id}/saveas/siteproduct", produces = MediaType.APPLICATION_JSON_VALUE) 
    public @ResponseBody ResponseEntity<CustomHttpResponse> saveAsSiteProduct(
      @PathVariable Long id, 
      @RequestParam Map<String, String> requestParams, 
      @RequestParam("guid") String guid 
      ) throws EncoderException, RestClientException { 
... 
} 

しかし、私は応答を得ています。

{ 
    "timestamp": "2017-10-02T21:21:58.064+0000", 
    "status": 400, 
    "error": "Bad Request", 
    "exception": "org.springframework.web.bind.MissingServletRequestParameterException", 
    "message": "Required String parameter 'guid' is not present", 
    "path": "/api/amazon/products/imported/1/saveas/siteproduct" 
} 

これは私が投稿しているものです。

{ 
createBrandIfNotExists : true 
createManufacturerIfNotExists : true 
guid : "asD" 
} 

私は郵便配達所でこれをテストしています。

答えて

0

代わりにURLSearchParamsを使用し、コンテンツタイプをapplication/x-www-form-urlencodedに変更しました。ここで私はそれをやったのです。

saveAsSiteProduct(id: number, data: saveAsSiteProductSettings): Observable<any> { 
    const options = new RequestOptions({ headers: new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' }) }); 
    const params = new URLSearchParams(); 
    for (var key in data) { 
     params.set(key, data[key]); 
    } 
    return this.http 
     .post(this.amazonUrl + '/products/imported/' + id +'/saveas/siteproduct', params, options) 
     .map(response => response.json()); 
    } 
関連する問題