0

How to get json data response from Controller to JQueryAjax using Spring
I am trying to get json data from Controller but getting error ststus in JQueryAjax, Have any error in my code this is a simple login application

This is my Controller in Spring

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;
    }

This is my 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'           

                    }
                });

    }

I added dependency file

    <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>

My dispatcher-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>     

But i didnt get json response in ajax only getting error why?

Shows error in console

[http-nio-8080-exec-5] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Handler execution resulted in exception: Could not find acceptable representation

I got 406 error in my browser console too

1

3 Answers 3

0

spring mvc settings:

<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>
Sign up to request clarification or add additional context in comments.

5 Comments

I put this code in my dispatcher servlet got an error showing >cvc-complex-type.3.2.2: Attribute 'content-negotiation-manager' is not allowed to appear in element 'mvc:annotation-driven'.
I put this code in my dispatcher servlet.xml got an error showing cvc-complex-type.3.2.2: Attribute 'content-negotiation-manager' is not allowed to appear in element 'mvc:annotation-driven'
remove old <mvc:annotation-driven /> I missed a period
Not working Sir. 406 error in my browser console and I got error in my eclipse console [http-nio-8080-exec-9] WARN org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Handler execution resulted in exception: Could not find acceptable representation
Can you help me i didnt get it? I got 406 error is it any problem to my dependency file
0

add following code.....

 <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>

Comments

0

Sorry.Our time is probably the opposite.

remove your <mvc:annotation-driven/> and add code:

remove produces from your controller method.

<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>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.