2013-03-04 71 views
3

我想從我的彈簧mvc控制器返回JSONArray對象。但是我在瀏覽器上收到了406錯誤。這是一個Ajax請求,我使用jquery來提出請求。Spring MVC無法返回JSONArray對象錯誤406

春控制器的方法:

@RequestMapping("/getAuthorizedScreensForUser.do") 
public @ResponseBody JSONArray getAuthorizedScreensForUser(HttpSession session){ 
    UserAuthDetails userAuthDetails = (UserAuthDetails) session.getAttribute("userauthdetails"); 
    //This method returns JSONArray 
    return webUtilsService.getJSONArrayResponseForAuthorizedScreens(userAuthDetails.getScrnfldacss()); 
} 

我在春天context.xml中都嘗試這些條目

<mvc:annotation-driven> 
     <mvc:message-converters> 
      <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> 
       <property name="prefixJson" value="true"/> 
      </bean> 
     </mvc:message-converters> 
    </mvc:annotation-driven> 

<mvc:annotation-driven/> 

我的pom.xml有這些依賴關係:

<dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-core-asl</artifactId> 
      <version>1.9.12</version> 
     </dependency> 

     <dependency> 
      <groupId>org.codehaus.jackson</groupId> 
      <artifactId>jackson-mapper-asl</artifactId> 
      <version>1.9.12</version> 
     </dependency> 

我正在運行WAS 8.不知道是什麼問題。我能夠以json格式返回簡單對象,但不能返回JSONArray對象。請讓我知道如果我做錯了什麼。

我知道有這麼多現有的線程有相同的討論。但是我沒有找到任何具有JSONArray對象示例的人。 Pawan Chopra

回答

0

406意味着您的請求沒有包含與響應中匹配的接受標頭。這來自於W3

該請求所標識的資源只能能夠產生具有根據在請求中發送的接受報頭不能接受 內容特性 響應實體。

我會先看看請求開始。確保它的請求頭中有「application/json」。然後,確保您將響應的Content-Type設置爲相同。

+1

我已驗證。它的頭部有application/json。正如我所說的,當我使用JSONArray或JSONObject時,我正面臨着這個問題。任何其他對象工作正常。謝謝 – webdev 2013-03-04 22:40:00

+0

你驗證過產生的JSON是否正確?嘗試通過其中一個在線JSON驗證器發送它。我個人使用JSONlint:http://jsonlint.com/ – CodeChimp 2013-03-05 14:03:05

相關問題