无法使用Angle 6和Spring Boot执行编辑操作

时间:2018-10-09 07:35:19

标签: java spring spring-boot angular6

实际上我想以表格的形式执行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);
  }

我在春季启动时更新的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

1 个答案:

答案 0 :(得分:0)

您正在发送0作为值,但是该服务需要Selection对象!