I am getting "400 bad request" for sending post request to spring boot from angular

41 Views Asked by At

I tried sending the request postman as well but I am getting the same 400 bad request. All other mappings are working well.

Angular service call.

I am calling this "/update" request from updateWorkOutCategory as

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';

@Injectable({
  providedIn: 'root'
})
export class SetRecordApiService {

  constructor(private http: HttpClient) { }

  readonly serverURL : string="http://localhost:9092/";

  getAllWorkOutCategories() {
    return this.http.get(this.serverURL+"category");
  }

  addNewWorkOutCategory(category:any) {
    return this.http.post(this.serverURL+"category",category);
  }

  updateWorkOutCategory(category:any) {
    return this.http.post(this.serverURL+"category/update",category)
  }
}

component

saveWorkOutSet() {
      this.workOutSet.weight=this.weight;
      this.workOutSet.rep=this.reps;
      this.selectedWorkOut.workOutSets.push(this.workOutSet)
      this.workOutCategory.workOutsList[this.selectedWorkOutindex]=this.selectedWorkOut;
      this.updateWorkOutCategory(this.workOutCategory);
  }
updateWorkOutCategory(updatedWorkOutCategory:WorkOutCategory) {
    this.apiService.updateWorkOutCategory(updatedWorkOutCategory).subscribe((res:any)=> {
      this.workOutCategory=res;
      console.log(this.workOutCategory);
      alert(`saved ${this.newWorkOutName}`);
      this.enableWorkOutForm=!this.enableWorkOutForm;
    },(err=>{
      alert("error saving the record, try again!");
      console.log(err)
    }))

  }

spring boot :

@CrossOrigin
@RestController
@RequestMapping("/category")
public class WorkOutCategoryController {

    @Autowired
    private WorkOutCategoryService workOutCategoryService;
        @PostMapping()
        public ResponseEntity<WorkOutCategory> saveWorkOutCategory(@RequestBody WorkOutCategory workOutCategory) {
            return ResponseEntity.ok(workOutCategoryService.addWorkOutCategory(workOutCategory));
        }
        @GetMapping()
        public ResponseEntity<List<WorkOutCategory>> getAllWorkOutCategories() {
            return ResponseEntity.status(HttpStatus.OK).body(workOutCategoryService.getAllWorkOutCategories());
        }

        @PostMapping("/update")
        public ResponseEntity<WorkOutCategory> updateWorkOutCategory(@RequestBody WorkOutCategory workOutCategory) {
            return ResponseEntity.status(HttpStatus.OK).body(workOutCategoryService.updateWorkOutCategory(workOutCategory));
        }
}

impl:

   @Override
    public WorkOutCategory updateWorkOutCategory(WorkOutCategory workOutCategory) {
        WorkOutCategory origWorkOutCategory;
        origWorkOutCategory = workOutCategoryRepo.findById(workOutCategory.getCategoryId()).orElse(new WorkOutCategory());
        origWorkOutCategory.setCategoryName(workOutCategory.getCategoryName());
        origWorkOutCategory.setDescription(workOutCategory.getDescription());
        origWorkOutCategory.setWorkOutsList(workOutCategory.getWorkOutsList());
        return workOutCategoryRepo.save(origWorkOutCategory);
    }

body:

{
    "categoryId": "657d680e7c70787500bca410",
    "categoryName": "chest and Triceps",
    "workOutsList": [
        {
            "workOutName": "dumbell bench press",
            "workOutSets": [
                {
                    "weight": 10,
                    "rep": 18
                },
                {
                    "weight": "12.5",
                    "rep": "15"
                }
            ]
        },
        {
            "workOutName": "inclined dumbell bench press",
            "workOutSets": []
        },
        {
            "workOutName": "Overhead tricep extension",
            "workOutSets": []
        },
        {
            "workOutName": "tricep rope push down",
            "workOutSets": []
        },
        {
            "workOutName": "peck deck fly",
            "workOutSets": []
        }
    ],
    "description": "push day"
}

I am expecting that to be updated, I don't know what's causing 400 request, all other mappings are working fine, but this "/update" is not working from angular call as well as from post mapping.

0

There are 0 best solutions below