当使用 @RequestPart 注释方法调用 rest API 时,Rest Controller 的所有注入成员 bean 都是 NULL

All injected member bean of Rest Controller are NULL when invoking a rest API with @RequestPart annotated method

下面是我的 Rest Controller 代码:

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

问题是当我通过 POSTMAN 如下调用它时:

    curl --location --request POST 'http://localhost:8096/api/fileserver/add/file' --form 'files=@"/G:/My Drive/Money_receipt.jpg"'

我可以看到有效文件正在进入方法,但所有注入的 bean 都是 NULL Debug at the errornous method

我的环境: Java17 (zulu17.28.13-ca-jdk17.0.0-win_x64) Spring 引导:2.6.7

我终于找到了解决办法。这是一个愚蠢的错误,因为我将 API 方法 uploadFile 的访问修饰符声明为 private,如下所示:

private ResponseEntity<ResponseMessage<String>> uploadFile

将访问修饰符更改为 public 后,问题已解决:

public ResponseEntity<ResponseMessage<String>> uploadFile