SIn san sun
SIn san sun

Reputation: 623

My post method does not return data from the server after post

I have POST method who send data to the server and I want after that return data. When I subscribe on the post method:

  this.categoryService.storeCategory(this.currentFileUpload, this.category).subscribe(category => {
      console.log("podaci" + category);

    }
    );

This is my POST method in Angular:

 storeCategory(file: File, category: CategoryModel): Observable<HttpEvent<{}>> {
    const formdata: FormData = new FormData();
    console.log("1")
    formdata.append('file', file);
    formdata.append('category', new Blob([JSON.stringify({ "category_name": category.category_name, "category_description": category.category_description })], { type: "application/json" }))
    const url = `api/cateogry/saveCategory`;
    const req = new HttpRequest('POST', url, formdata, {
      reportProgress: true,
      responseType: 'text'
    });
    return this.http.request<CategoryModel[]>(req);
  }

This is my POST method in Spring Boot:

@PostMapping(consumes = { "multipart/form-data" }, value = "/saveCategory")
    @ResponseStatus(HttpStatus.OK)
    public List<CategoryModel> createCategory(@RequestPart("category") @Valid CategoryModel category,
            @RequestPart("file") @Valid @NotNull MultipartFile file) {
        String fileName = fileStorageService.storeFile(file);

        String workingDir = System.getProperty("user.dir") + "/uploads/";

        category.setImage_path(workingDir + fileName);
        this.fileStorageService.storeFile(file);

        /*
         * String fileURL =
         * ServletUriComponentsBuilder.fromCurrentContextPath().path("/uploads/").path(
         * fileName) .toUriString();
         */

        this.categoryRepository.insertCategory(category.getCategory_description(), category.getImage_path(),
                category.getCategory_name());
        return this.categoryRepository.findAll();
    }

Upvotes: 0

Views: 45

Answers (1)

Amit K Bist
Amit K Bist

Reputation: 6818

@ResponseBody is missing in your controller createCategory method. The @ResponseBody annotation tells a controller that the object returned is automatically serialized into JSON and passed back into the HttpResponse object. Check this for more details.

Upvotes: 2

Related Questions