0

如何使用 Spring 获取从 Controller 到 JQueryAjax 的 json 数据
响应

这是我在 Spring 中的控制器

家庭控制器.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

数据.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] 警告 org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - 处理程序执行导致异常:找不到可接受的表示

我的浏览器控制台也出现 406 错误

4

3 回答 3

0

春天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>
于 2016-05-07T08:29:16.483 回答
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>
于 2016-05-07T08:47:21.760 回答
0

抱歉,我们的时间可能正好相反。

删除您的<mvc:annotation-driven/>并添加代码:

从您的控制器方法中删除产品。

<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>
于 2016-05-08T00:17:36.843 回答