2011-12-06 61 views
5

我正在使用Spring MVC 3構建帶有REST風格的Web服務的Web應用程序.Web服務將被應用程序使用,因此不應該真正解決對視圖的任何請求。有什麼辦法可以在servlet上下文中指定任何請求都不應該解析爲任何視圖?不要在REST風格的應用程序中解析視圖

目前,我有:

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/jsp/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 

我知道試圖解決在jsp文件夾中的相應的命名視圖的任何請求。但是,如果我刪除它,應用程序只會嘗試使用默認視圖解析器。

我很擔心這種情況的原因是,我的應用程序日誌將是充滿了以下消息(即使它工作正常):

SEVERE: Servlet.service() for servlet [DispatcherServlet] in context with path [/vouchd] threw exception [Circular view path [signup]: would dispatch back to the current handler URL [/vouchd/signup] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.)] with root cause 
javax.servlet.ServletException: Circular view path [signup]: would dispatch back to the current handler URL [/vouchd/signup] again. Check your ViewResolver setup! (Hint: This may be the result of an unspecified view, due to default view name generation.) 

或與InternalViewResolver

WARN [http-bio-8080-exec-4] (DispatcherServlet.java:1057) - No mapping found for HTTP request with URI [/app/WEB-INF/jsp/call.jsp] in DispatcherServlet with name 'DispatcherServlet' 

我猜這是兩個弊端中最好的。我不想關閉日誌記錄WARN級別。

回答

7

試用@ResponseStatus。此代碼返回204,沒有內容和查看解決所忽略:

@ResponseStatus(NO_CONTENT) 
void noView() { 
    //... 
} 

如果你想返回原始數據和簡單地將其序列化到JSON或XML,使用@ResponseBody

@ResponseBody 
MyPojo noView() { 
    return new MyPojo(); 
} 
+0

我仍然返回JSON數據到請求的應用程序。這不會否定嗎? –

+0

@NicoHuysamen:對不起,我誤解*沒有請求應該resvle到任何視圖*,看到我的更新。 –

+0

真棒,謝謝! –

相關問題