spring mvc not returning json content - error 406

后端 未结 14 1275
温柔的废话
温柔的废话 2020-11-30 05:10

I am using Spring MVC with JSON as specified in Ajax Simplification Spring 3.0 article.

After so many attempts and variations of my code depending on advice found on

相关标签:
14条回答
  • 2020-11-30 05:29

    Instead of @RequestMapping(...headers="Accept=application/json"...) use @RequestMapping(... , produces = "application/json")

    0 讨论(0)
  • 2020-11-30 05:32

    I also faced this same issue and I downloaded this [jar]: (http://www.java2s.com/Code/Jar/j/Downloadjacksonall190jar.htm)! and placed in lib folder and the app works like a charm :)

    0 讨论(0)
  • 2020-11-30 05:33

    As said by axtavt, mvc:annotation-driven and jackson JSON mapper are all that you need. I followed that and got my application to return both JSON and XML strings from the same method without changing any code, provided that there are @XmlRootElement and @XmlElement in the object you are returning from the controller. The difference was in the accept parameter passed in the request or header. To return xml, any normal invocation from the browser will do it, otherwise pass the accept as 'application/xml'. If you want JSON returned, use 'application/json' in the accept parameter in request.

    If you use firefox, you can use tamperdata and change this parameter

    0 讨论(0)
  • 2020-11-30 05:35

    Add org.springframework.http.converter.json.MappingJacksonHttpMessageConverter and org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter to DispatcherServlet-servlet.xml. and refer to the the first one in the second using

    <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
        <property name="messageConverters">
            <list>
                <ref bean="jacksonMessageConverter"/>
            </list>
        </property>
    </bean>
    
    0 讨论(0)
  • 2020-11-30 05:37

    issue is not related to jquery . even bug is saying it is server side issue . please make sure that following 2 jar present in class path :-

    jackson-core-asl-1.9.X.jar jackson-mapper-asl-1.9.X.jar

    0 讨论(0)
  • 2020-11-30 05:41

    To return JSON response from @ResponseBody-annotated method, you need two things:

    • <mvc:annotation-driven /> (you already have it)
    • Jackson JSON Mapper in the classpath

    You don't need ContentNegotiatingViewResolver and headers in @RequestMapping.

    0 讨论(0)
提交回复
热议问题