2016-05-07 85 views
0

如何使用Spring
我想從控制器獲取JSON數據,但得到的JQueryAjax錯誤ststus從控制器獲取JSON數據響應JQueryAjax, 在我的代碼中的任何錯誤,這是一個簡單的登錄應用如何使用Spring獲取從Controller到JQueryAjax的json數據響應?

這是我在春天

控制器

HomeController.java

@RequestMapping(value = "/login.htm", method = RequestMethod.POST, produces = "application/json") 
    public @ResponseBody User loginUser(HttpServletRequest request, HttpServletResponse response, User ub) 
    { 

     String email = request.getParameter("txt_email"); 
     String password = request.getParameter("txt_password"); 

     ub.setEmail(email); 
     ub.setPassword(password); 

     UserServiceImpl us = new UserServiceImpl(); 
     User ub1= us.verifyUserLogin(ub); 

     return ub1; 
    } 

這是我JQueryAjax

個data.js

function usersignin(url) 
    { 
     var val = signin_validate(); 
     if (val == false) 
     { 
      return; 
     } 
     var email = $('#txt_email').val(); 
     var password = $('#txt_password').val(); 
     var formData = 
     { 
       'txt_email' : email, 
       'txt_password' : password, 

     }; 
     $.ajax(
       { 
        type : 'POST', 
        url : url, 
        data : formData, 
        dataType : 'json', 

        success : function(res, textStatus) 
        { 
         var msg="Succesfully..! Login"; 
         showAlertLogin(msg); 
         window.location.href='index.jsp'    

        }, 

        error : function(res, textStatus) 
        { 
         var msg="Failed..! Login"; 
         showAlertLogin(msg); 
         window.location.href='layout.jsp'   

        } 
       }); 

    } 

我添加依賴文件

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

    <!-- Jackson JSON Mapper --> 
    <dependency> 
     <groupId>org.codehaus.jackson</groupId> 
     <artifactId>jackson-mapper-asl</artifactId> 
     <version>1.9.10</version> 
    </dependency> 

    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-core</artifactId> 
     <version>2.5.1</version> 
    </dependency> 

    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-databind</artifactId> 
     <version>2.5.1</version> 
    </dependency> 
    <dependency> 
     <groupId>com.fasterxml.jackson.core</groupId> 
     <artifactId>jackson-annotations</artifactId> 
     <version>2.5.1</version> 
    </dependency> 

    <dependency> 
     <groupId>com.fasterxml.jackson.jaxrs</groupId> 
     <artifactId>jackson-jaxrs-base</artifactId> 
     <version>2.6.1</version> 
    </dependency> 

我的調度員servlet.xml中

<mvc:annotation-driven /> 

<context:annotation-config/> 

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> 
    <property name="mediaTypes"> 
     <map> 
      <entry key="xml" value="application/xml"/> 
      <entry key="json" value="application/json"/> 
     </map> 
    </property> 
    <property name="ignoreAcceptHeader" value="true"/> 
    <property name="favorPathExtension" value="true"/> 
</bean>  

但我沒有得到的AJAX JSON響應只得到錯誤,爲什麼?

顯示錯誤在控制檯

[HTTP-NIO-8080-EXEC-5] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - 處理程序執行導致異常:無法找到可接受表示

我在瀏覽器控制檯得到406錯誤太

+0

試試這個http://stackoverflow.com/questions/7197268/spring-mvc-httpmediatypenotacceptableexception –

回答

0

Spring MVC的設置:

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"> 
      <mvc:message-converters register-defaults="true"> 
       <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 
        <constructor-arg value="UTF-8"/> 
       </bean> 
       <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
        <property name="supportedMediaTypes"> 
         <list> 
          <value>application/json;charset=UTF-8</value> 
          <value>text/html;charset=UTF-8</value> 
         </list> 
        </property> 
       </bean> 
      </mvc:message-converters> 
     </mvc:annotation-driven> 
+0

我把這個代碼在我的分發程序Servlet顯示了> CVC-複雜type.3.2.2錯誤:屬性'content-negotiation-manager'不允許出現在元素'mvc:annotation-driven'中。 –

+0

我把這段代碼放在我的調度程序servlet.xml中顯示錯誤,顯示** cvc-complex-type.3.2.2:屬性'content-negotiation-manager'不允許出現在元素'mvc:annotation-driven'中* * –

+0

刪除舊的我錯過了一段時間 – zhangbowen

0

添加以下代碼.....

<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> 
      <property name="mediaTypes"> 
       <map> 
        <entry key="xml" value="application/xml"/> 
        <entry key="json" value="application/json"/> 
       </map> 
      </property> 
      <property name="ignoreAcceptHeader" value="true"/> 
      <property name="favorPathExtension" value="true"/> 
     </bean> 
0

對不起。我們的時間可能正好相反。

移除<mvc:annotation-driven/>並添加代碼:

remove從控制器方法產生。

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager"> 
    <mvc:message-converters register-defaults="true"> 

     <bean class="org.springframework.http.converter.StringHttpMessageConverter"> 
      <constructor-arg value="UTF-8"/> 
     </bean> 

     <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> 
      <property name="supportedMediaTypes"> 
       <list> 
        <value>application/json;charset=UTF-8</value> 
        <value>text/html;charset=UTF-8</value> 
       </list> 
      </property> 
      <property name="prettyPrint" value="false"/> 
      <property name="objectMapper"> 
       <bean class="spider.common.mapper.JsonMapper"></bean> 
      </property> 
     </bean> 
    </mvc:message-converters> 
</mvc:annotation-driven> 


<bean id="contentNegotiationManager" class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean"> 
    <property name="mediaTypes"> 
     <map> 
      <entry key="xml" value="application/xml"/> 
      <entry key="json" value="application/json"/> 
     </map> 
    </property> 
    <property name="ignoreAcceptHeader" value="true"/> 
    <property name="favorPathExtension" value="true"/> 
</bean>