Below is my Rest Controller Code:
package com.tripura.fileserver.controller;
import com.magic.fileserver.annotation.TrackTime;
import com.magic.fileserver.model.ResponseMessage;
import com.magic.fileserver.service.S3Factory;
import com.magic.fileserver.service.SequenceGeneratorService;
import com.magic.fileserver.service.StorageService;
import org.springframework.http.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.security.Principal;
import java.util.List;
@RestController
@RequestMapping(FileServerController.ROOT_MAPPING)
public class FileServerController {
public static final String ROOT_MAPPING = "/api/fileserver";
private final SequenceGeneratorService sequenceGeneratorService;
private final StorageService storageService;
private final S3Factory s3Factory;
public FileServerController(SequenceGeneratorService sequenceGeneratorService, StorageService storageService, S3Factory s3Factory) {
this.sequenceGeneratorService = sequenceGeneratorService;
this.storageService = storageService;
this.s3Factory = s3Factory;
}
@GetMapping(value = "/health/admin")
@PreAuthorize("hasRole('ADMIN')")
@TrackTime
public ResponseEntity<?> checkRequestAdmin() {
return new ResponseEntity<>("Hello admin", HttpStatus.OK);
}
@PostMapping(value = "/add/file", consumes = {MediaType.MULTIPART_FORM_DATA_VALUE})
@PreAuthorize("hasRole('USER') or hasRole('CONSULTANT') or hasRole('ADMIN')")
@TrackTime
private ResponseEntity<ResponseMessage<String>> uploadFile(
@RequestPart("files") List<MultipartFile> files) {
// this.storageService.storeFileOnS3Bucket(files.get(0), image, fileName,
// userName, category);
ResponseMessage<String> responseMessage = new ResponseMessage<>();
responseMessage.setMessage("File added: " + files.get(0).getOriginalFilename());
return new ResponseEntity<>(responseMessage, HttpStatus.OK);
}
Problem is when i am invoking it via POSTMAN as below:
curl --location --request POST 'http://localhost:8096/api/fileserver/add/file' --form 'files=@"/G:/My Drive/Money_receipt.jpg"'
I can see valid file is coming inside the method but all the injected beans are NULL Debug at the errornous method
My Environment: Java 17 (zulu17.28.13-ca-jdk17.0.0-win_x64) Spring Boot: 2.6.7
Finally i found the solution. It was a silly mistake as i declared access modifier for the API method uploadFile as private as below:
private ResponseEntity<ResponseMessage<String>> uploadFile
After changing the access modifier to public, it has resolved the issue:
public ResponseEntity<ResponseMessage<String>> uploadFile