2012-04-21 51 views
0

對於測試的原因,我想有一個彈簧MVC控制器與一個返回值應該由彈簧忽視的方法簽名:控制器的返回值應被忽略

@RequestMapping(value = "/{uuid}", params = "question", method = RequestMethod.POST) 
@ResponseBody 
public IAnswer saveAnswer(UiAnswer json, @PathVariable String uuid, @RequestParam("question") String question, HttpServletResponse response) {} 

這是一個職位 - 請求控制器收到數據,如果一切正常,只需要response.setStatus(HttpServletResponse.SC_OK);

  • 隨着@ResponseBody它死在一個內部服務器錯誤500
  • 沒有@ResponseBody春天嘗試解析視圖
  • 隨着void的返回值,它工作正常

我可以告訴春天忽略我返回的對象? 謝謝!

回答

0
@RequestMapping(value = "/{uuid}", params = "question", method = RequestMethod.POST) 
@ResponseBody 
public void saveAnswer(UiAnswer json, 
         @PathVariable String uuid, 
         @RequestParam("question") String question, 
         HttpServletResponse response) { 
    doSaveAnswer(json, uuid, question, response); 
} 

protected IAnswer doSaveAnswer(UiAnswer json, 
           String uuid, 
           String question, 
           HttpServletResponse response) { 
    // your original code here 
} 

然後單元測試doSaveAnswer()

+0

這是一個解決方案,謝謝! – myborobudur 2012-04-21 15:39:09

+0

嗯看起來像一個典型的服務實現...也許OP應該使用服務層,不僅更容易測試,但你得到N層架構的所有其他好處... – nickdos 2012-04-21 23:10:37

+0

你是對的,我應該將一些邏輯移到服務層 – myborobudur 2012-04-23 12:31:27