2013-02-05 37 views
6

在我的春天的webapp,我有一個AJAX的servlet這個問題的答案(使用傑克遜)JSON:春天阿賈克斯​​ - @ResponseBody - 返回null響應

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:util="http://www.springframework.org/schema/util" xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd 
     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"> 

    <context:component-scan base-package="com.myapp.ajax" /> 

    <util:list id="messageConvertersList"> 
     <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
      <property name="supportedMediaTypes"> 
       <list> 
        <value>application/json;charset=UTF-8</value> 
       </list> 
      </property> 
     </bean> 
    </util:list> 

    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
     <property name="messageConverters" ref="messageConvertersList" /> 
    </bean> 
    <bean id="handlerMapping" class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" /> 

    <mvc:interceptors> 
     <mvc:interceptor> 
      <mvc:mapping path="/**" /> 
      <bean id="localeChangeInterceptor" class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"> 
       <property name="paramName" value="lang" /> 
      </bean> 
     </mvc:interceptor> 
    </mvc:interceptors> 

    <bean id="localeResolver" class="org.springframework.web.servlet.i18n.SessionLocaleResolver" /> 

    <bean id="handlerExceptionResolver" class="com.myapp.ajax.AjaxExceptionHandlerResolver"> 
     <property name="exceptionHandler"> 
      <bean class="com.myapp.ajax.AjaxExceptionHandler" /> 
     </property> 
     <property name="messageConverters" ref="messageConvertersList" /> 
    </bean> 

我有以下的AJAX服務:

@RequestMapping(value = "getLoggedUser") 
@ResponseBody 
public DtoUser getLoggedUser() { 
    return authenticationService.getLoggedUser(); 
} 

當用戶登錄其返回類似:

{ userName : "jojo", email : "[email protected]", firstName : "John", lastName : "Doe" } 

當用戶沒有登錄,預期的行爲是返回

null 

但是它返回一個空應答,其不是有效的JSON響應(並另外用壞內容類型報頭)

這是怎麼發生的?
我是否有解決方案來獲得預期的行爲?

+0

我會認爲空的json會是正確的行爲,如果你返回一個空對象,不返回字符串「null」。什麼是作爲內容類型返回?如果您試圖遵循更加REST的模式,最好的解決方案是返回適當的HTTP狀態代碼,如404(未找到)或401(未授權)。或者,更好的是,將它們重定向/轉發到登錄頁面。 – CodeChimp

+0

一個空的json意味着我必須測試一個屬性(例如:response.email)以知道用戶是否爲null。這就是爲什麼我認爲返回null更合適和連貫 –

回答

6

當用戶沒有登錄,預期的行爲是返回空

這是我的正常現象,因爲在Java和Javascript/JSON,null是一個有效的值,它有一個不同於空的意思,空或錯誤/異常。

我希望Spring回答空響應,而不是專門處理它。 在這種情況下,爲空(Java)的預期皈依是空(JSON)
我的預期轉換表:

Java Exception => HTTP Error Code 
null => null 
empty map/object => {} 
void => no response 


這究竟是爲什麼?

對於Spring,控制器返回null意味着「沒有響應」,而不是「一個響應哪個值爲null」。此規則適用於所有控制器方法,包括帶有@ResponseBody註釋的控制器方法。
這允許手動寫入響應,而無需附加的東西以後的響應:

if (mySpecialCase) { 
    Writer writer = response.getWriter(); 
    writer.write("my custom response"); 
    return null; 
} else { 
    return myObject; 
} 

所以到響應,也不是Content-type頭也不體返回null春寫什麼時。

我是否有解決方案來獲得預期的行爲?

我做了下面的骯髒的黑客:添加一個過濾器在我的ajax路徑,當沒有響應被提交時寫入null到響應。

public class AjaxEmptyResponseFilter implements Filter { 

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 
     chain.doFilter(request, response); 
     if (!response.isCommitted()) { 
      response.setCharacterEncoding("UTF-8"); 
      response.setContentType("application/json"); 
      Writer writer = response.getWriter(); 
      writer.write("null"); 
      writer.close(); 
      response.flushBuffer(); 
     } 
    } 

} 

此解決方案處理方法回答空和方法回答什麼(空)相同的方式。

0

你有會話過濾器嗎?

我認爲,你可以綁定一個全局ajax事件的錯誤,並在那裏進行各自的驗證。

這裏是一個類似的情況下,例如:How to handle expired session using spring-security and jQuery?

+0

我的問題並不是專門用於登錄/會話管理。我想知道如何用spring ajax返回「null」(我的意思不是「沒有迴應」,而是迴應說「沒有結果」)。 –