spring POST /导入无Map

1l5u6lss  于 2023-03-07  发布在  Spring
关注(0)|答案(1)|浏览(106)

请帮助,如果你有任何想法,为什么它不工作。我一直试图测试它与 Postman 几个小时,但没有运气。
导入控制器:

package demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.web.multipart.MultipartFile;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
import javax.annotation.PostConstruct;
import javax.servlet.annotation.MultipartConfig;

@RestController
@RequestMapping("/imports")
@MultipartConfig(fileSizeThreshold = 20971520) //20MB
public class ImportController {
    @Autowired
    private ImportRepository importRepository;

    @PostConstruct
    public void init() {
        System.out.println("ImportController initialized.");
    }

    // Create a new import
    @PostMapping
    @RequestMapping(value = "/", method = RequestMethod.POST, consumes = "multipart/form-data")
    public ImportModel createImport(@RequestParam("file") MultipartFile file) throws IOException {
        // Save the file to the server's file system
        Path filePath = Paths.get("", file.getOriginalFilename());
        file.transferTo(filePath);

        // Create an ImportModel object with the file path and save it to the database
        ImportModel importModel = new ImportModel();
        importModel.setFilename(file.getOriginalFilename());
        importModel.setFilePath(filePath.toString());
        return importRepository.save(importModel);
    }

这是具有请求并且应该具有Map的控制器

elcex8rz

elcex8rz1#

您的代码需要“/imports/"。
在课堂上:

@RequestMapping("/imports")

然后是方法

@PostMapping
@RequestMapping(value = "/", . . . .

忽略第二个@RequestMapping,只使用

@PostMapping(consumes = "multipart/form-data")

真的没有必要把它们都混在一起,做双重定义。

相关问题