簡體   English   中英

無法使用Angle 6和Spring Boot執行編輯操作

[英]Not able to perform edit operation using angular 6 and spring boot

實際上我想在表單中執行Edit操作。我將Id傳遞給我在forntend中使用angular 6制作的Spring boot Api,但出現以下錯誤:

調用更新方法的主要代碼:

{
      this.selectService.updatenewSelection(this.selection.selectionId,0).subscribe((selection)=>{
         console.log(selection);
         this.router.navigate(['/add-selection']);
       },(error)=>{
         console.log(error);
       });
    }

現在,selection.service.ts中的更新方法是

updatenewSelection(id: number, value: any): Observable<Object> {
    return this.http.put(`${this.baseUrl}/${id}`, value);
  }

我在Spring Boot中進行更新的api是:我嘗試了兩種方法,但仍無法正常工作。

 @PutMapping("/selections/{id}")
        public ResponseEntity<Selection> updateSelection(@PathVariable("id") long id, @RequestBody Selection selection) {
            System.out.println("Update Selection with ID = " + id + "...");

            Optional<Selection> selectionData = repository.findById(id);

            if (selectionData.isPresent()) {
                Selection _selection = selectionData.get();
                _selection.setSelectionDate(selection.getSelectionDate());
                _selection.setSelectedBy(selection.getSelectedBy());


                return new ResponseEntity<>(repository.save(_selection), HttpStatus.OK);
            } else {
                return new ResponseEntity<>(HttpStatus.NOT_FOUND);
            }
        }

        @PutMapping("/selections/update")
        public Selection updatenewSelection(@RequestBody Selection selection) {
            return repository.save(selection);
        }

單擊保存按鈕時,出現錯誤,即“ 1”是傳遞的ID:

 PUT http://localhost:8080/api/selections/1 400
    HttpErrorResponse {headers: HttpHeaders, status: 400, statusText: "OK", url: "http://localhost:8080/api/selections/1", ok: false, …}
    error: {timestamp: "2018-10-09T07:29:16.628+0000", status: 400, error: "Bad Request", message: "JSON parse error: Cannot construct instance of `co…ource: (PushbackInputStream); line: 1, column: 1]", path: "/api/selections/1"}
    headers: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, lazyInit: ƒ}
    message: "Http failure response for http://localhost:8080/api/selections/1: 400 OK"
    name: "HttpErrorResponse"
    ok: false
    status: 400
    statusText: "OK"
    url: "http://localhost:8080/api/selections/1"
    __proto__: HttpResponseBase

您正在發送0作為值,但是該服務需要Selection對象!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM