此方法调用为非空方法参数传递空值。要么参数被注释为应该始终为非空的参数

This method call passes a null value for a nonnull method parameter. Either the parameter is annotated as a parameter that should always be nonnull

以下方法的 SonarQube 错误,请专家就如何解决该问题提出任何建议 - 此方法调用为非空方法参数传递空值。参数要么被注释为应始终为非空的参数,要么分析表明它将始终被取消引用。

    public ByteArrayResource readFile() throws IOException {
        byte[] content = null;

        try (S3Object object = amazonS3.getObject(new GetObjectRequest(bucketName, key))) {
            content = IOUtils.toByteArray(object.getObjectContent());
            return new ByteArrayResource(content);

        } catch (IOException e) {
            LOG.error("IOException caught while reading file", e);
        } 
        return new ByteArrayResource(content);
    }

问题出在 try/catch 块之外的 return new ByteArrayResource(content); 语句。由于您的方法正在抛出 IOException,因此您不应该捕获它。下面应该解决它:

public ByteArrayResource readFile() throws IOException {
    try (S3Object object = amazonS3.getObject(new GetObjectRequest(bucketName, key))) {
        byte[] content = IOUtils.toByteArray(object.getObjectContent());
        return new ByteArrayResource(content);
    }
}