spring mvc not returning json content - error 406

后端 未结 14 1296
温柔的废话
温柔的废话 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:28

    Well, The answers on this page might be right but they didn't expatiate well. This is what I did

    I added this to my pom.xml

    
        org.codehaus.jackson
        jackson-core-asl
        1.9.8
    
    
        org.codehaus.jackson
        jackson-mapper-asl
        1.9.8  
    
    

    Then I added headers to my RequestMapping like below

    @RequestMapping(value="/admin/getGallery", method = RequestMethod.GET, headers={"Content-Type=application/json"})
    

    Then in my jquery ajax I added - contentType: "application/json", so it looks like

    jQuery.ajax({
                type:'GET',
                url:"getGallery.html",
                data: "designId="+designId,
                processData:false,
                contentType: "application/json",
                //dataType: "json",
               success:function(data){
                  console.log(data);
    
               },
                error : function(e) {
                    console.log("ERROR: ", e);
                },
            });
    

    Then in my servlet I added

    
        
        
        
        
        
        
        
     
    

    If you have problem with util tag in your servlet just just add in the same servlet file

    xmlns:util="http://www.springframework.org/schema/util"
    

    and

    xsi:schemaLocation="http://www.springframework.org/schema/util
    http://www.springframework.org/schema/util/spring-util-3.0.xsd
    

提交回复
热议问题