【问题标题】:Post Request return 404 for Spring Boot with Postman使用 Postman 为 Spring Boot 发布请求返回 404
【发布时间】:2019-09-02 19:23:36
【问题描述】:

我正在尝试使用邮递员来测试我为我的 Spring Boot 应用程序创建的一个发布请求。我通过邮递员的发帖请求总是返回 404。

  • 我已经为 get 请求创建了一个相同的映射路径,并且使用邮递员,get 请求按预期工作。
  • 我已经使用 aws cli 进行了测试,并确保我拥有正确的访问密钥和密钥,用于将文件上传到 S3。

我的服务代码

@Service
public class AmazonClient {

    private AmazonS3 s3client;

    @Value("${amazonProperties.endpointUrl}")
    private String endpointUrl;
    @Value("${amazonProperties.bucketName}")
    private String bucketName;
    @Value("${amazonProperties.accessKey}")
    private String accessKey;
    @Value("${amazonProperties.secretKey}")
    private String secretKey;

    @PostConstruct
    private void initializeAmazon() {
        AWSCredentials credentials = new BasicAWSCredentials(this.accessKey, this.secretKey);
        this.s3client = AmazonS3ClientBuilder.standard().withRegion(Regions.US_EAST_2).withCredentials(
                new AWSStaticCredentialsProvider(credentials)).build();
    }

    @Async
    public String uploadFile(MultipartFile multipartFile, boolean enablePublicReadAccess) {
        String fileUrl = "";
        System.out.println("Reach");
        try {
            File file = convertMultiPartToFile(multipartFile);
            String fileName = generateFileName(multipartFile);
            System.out.println("FileName: " + fileName);
            fileUrl = endpointUrl + "/" + bucketName + "/" + fileName;
            PutObjectRequest putObjectRequest = new PutObjectRequest(this.bucketName, fileName, file);

            if (enablePublicReadAccess) {
                putObjectRequest.withCannedAcl(CannedAccessControlList.PublicRead);
            }
            s3client.putObject(putObjectRequest);
            file.delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return fileUrl;
    }

    private File convertMultiPartToFile(MultipartFile file) throws IOException {
        File convFile = new File(file.getOriginalFilename());
        FileOutputStream fos = new FileOutputStream(convFile);
        fos.write(file.getBytes());
        fos.close();
        return convFile;
    }

    private String generateFileName(MultipartFile multiPart) {
        return new Date().getTime() + "-" + multiPart.getOriginalFilename().replace(" ", "_");
    }


    public String deleteFileFromS3Bucket(String fileUrl) {
        String fileName = fileUrl.substring(fileUrl.lastIndexOf("/") + 1);
        s3client.deleteObject(new DeleteObjectRequest(bucketName, fileName));
        return "Successfully deleted";
    }
}

我的控制器的代码:


@RestController
@RequestMapping("/storage/files")
public class BucketController {

    private AmazonClient amazonClient;

    @Autowired
    BucketController(AmazonClient amazonClient) {
        this.amazonClient = amazonClient;
    }


    @GetMapping
    public String getFile(){
        return "Files";
    }

    @PostMapping("/file")
    public String file() {
        return "Reach!";
    }

    @PostMapping
    public String uploadFile(@RequestPart(value = "file") MultipartFile file) {
        System.out.println("Reach!!");
        return this.amazonClient.uploadFile(file, true);
    }

    @DeleteMapping
    public String deleteFile(@RequestPart(value = "url") String fileUrl) {
        return this.amazonClient.deleteFileFromS3Bucket(fileUrl);
    }
}

我的安全配置:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/css/**", "/js/**", "/fonts/**", "/index").permitAll()
                .antMatchers("/storage*").permitAll();

通过邮递员,我选择了一个 POST 请求并将http://localhost:8080/storage/files/file 放入正文中,我输入了一个键“文件”并将值设置为文件类型并从本地选择一个文件。 回复如下:

{
    "timestamp": "2019-09-02T19:09:54.864+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/storage/files/file"
}

Project Structure

Postman Results

【问题讨论】:

    标签: spring amazon-web-services spring-boot amazon-s3


    【解决方案1】:

    这几乎肯定是您的安全配置在干扰。

    您是否尝试过:.antMatchers("/storage/**")

    【讨论】:

    • 感谢您的回答,我已尝试使用 /storage/** 并且仍然遇到相同的错误,需要提及的一件事是我能够从相同的 get API 调用。
    • 根据您的控制器,您应该只收到GET /storage/files 的响应,而不是GET /storage/files/file。这不是苹果对苹果的比较
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-09
    • 1970-01-01
    • 2015-05-13
    • 2017-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多