简体   繁体   中英

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

Actually i want to perform the Edit operation in the form.I am passing the Id to the Spring boot Api i have made using angular 6 in forntend but I am getting the error as:

The main code to call update method:

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

Now the update method in selection.service.ts is

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

The api i have made to update in spring boot is:I have tried both method but it is not still working.

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

I get error when clicked the save button is where the "1" is the Id it is passing:

 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对象!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM