【问题标题】:creating an upload functionality in spring, getting error threw exception [Handler processing failed; nested exception is java.lang.StackOverflowError在春季创建上传功能,出错抛出异常[处理程序处理失败;嵌套异常是 java.lang.StackOverflowError
【发布时间】:2016-12-13 22:37:29
【问题描述】:

相关文件-migrationruleserviceimpl

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;

import javax.inject.Named;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

@Named
public class MigrationRuleServiceImpl implements MigrationRuleService {

@Override
    @Transactional(propagation=Propagation.REQUIRED)
    public String migrationRuleUpload(MultipartFile file){

         if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    BufferedOutputStream stream =
                            new BufferedOutputStream(new FileOutputStream(new File("lul")));
                    stream.write(bytes);
                    stream.close();
                    return "You successfully uploaded ";

                } catch (Exception e) {
                    return "You failed to upload  => " + e.getMessage();
                }
            } else {
                return "You failed to upload because the file was empty.";
            }
    }
}

迁移规则服务

import org.springframework.web.multipart.MultipartFile;

public interface MigrationRuleService {


    String migrationRuleUpload(MultipartFile file);

}

迁移规则控制器

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping(value = "/upload")
public class MigrationRuleController {



    @RequestMapping(method = RequestMethod.POST)
    public String migrationRuleUpload(@RequestParam("file") MultipartFile file) {
        return migrationRuleUpload(file);
    }

}

当我在邮递员中尝试此操作时,我收到内部服务器错误 500 - 来自 tomcat 日志的错误消息:

SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] threw exception [Handler processing failed; nested exception is java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError at MigrationRuleController.migrationRuleUpload(MigrationRuleController.java:17)
<the previous line repeated bazilion of times>

这可能有什么问题?

【问题讨论】:

  • 忘了补充,MigrationRuleController.migrationRuleUpload(MigrationRuleController.java:17) 的错误行java.lang.StackOverflowError 重复了50次,不知道是否相关

标签: java spring file-upload controller postman


【解决方案1】:

原因很清楚——你递归地调用了一些函数。这是罪魁祸首:

public String migrationRuleUpload(@RequestParam("file") MultipartFile file) {
    return migrationRuleUpload(file);
}

这个方法只是调用自己。相反,您可能想注入 MigrationRuleService 实例并在该实例上调用 migrationRuleUpload

public class MigrationRuleController {
    @Inject
    private MigrationRuleService migrationRuleService;

    @RequestMapping(method = RequestMethod.POST)
    public String migrationRuleUpload(@RequestParam("file") MultipartFile file) {
        return migrationRuleService.migrationRuleUpload(file);
    }
}

【讨论】:

  • 如此明显的错误-谢谢。我什至还有其他类似的工作方法,每个人都调用了服务,只需要查找它们就可以了。我不知道你是谁,在哪里,但我会找到你……我会吻你。
猜你喜欢
  • 2016-12-12
  • 1970-01-01
  • 1970-01-01
  • 2014-12-13
  • 1970-01-01
  • 2011-12-12
  • 2021-10-06
  • 2018-05-04
  • 2017-11-30
相关资源
最近更新 更多