2016-08-08 56 views
1

相關的文件 - migrationruleserviceimpl在春季創建上傳功能,出現異常拋出異常[Handler processing failed;嵌套的例外是java.lang.StackOverflowError的

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."; 
      } 
    } 
} 

migrationruleservice

import org.springframework.web.multipart.MultipartFile; 

public interface MigrationRuleService { 


    String migrationRuleUpload(MultipartFile file); 

} 

migrationrulecontroller

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> 

這可能有什麼問題嗎?

+0

忘了補充,在MigrationRuleController.migrationRuleUpload(MigrationRuleController.java:17)錯誤行java.lang.StackOverflowError的重複就像50次,不知道其相關的 – Entman

回答

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

這樣一個明顯的錯誤 - 謝謝。我甚至還有其他類似的工作方法,他們中的每一個人都打電話給服務,只需要查看他們的LOL。我不知道你是誰,但是我會找到你......我會吻你。 – Entman