6

I am having problem with my jQuery function what I am trying to achieve is to populate data in a listbox

The JavaScript function

function load() {
        $.getJSON('${findAdminGroupsURL}', {
            ajax : 'true'
        }, function(data) {
            var html = '<option value="">Groups</option>';
            var len = data.length;
            for ( var i = 0; i < len; i++) {
                html += '<option value="' + data[i].name + '">' + data[i].name
                        + '</option>';
            }
            html += '</option>';

            $('#selection').html(html);
        });
    }

The server side is

@RequestMapping(value = "groups", method = RequestMethod.GET)
    public @ResponseBody
    List<Group> getGroups() {
        return this.businessGroups();
    }

I call load() function on load it triggers the function getGroups() and returns the list successfully but the problem is once the getGroups() is finished

function(data) doesn't load never gets into that function and the error is

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

Can't I post back a list of Group objects, or does it have to be a Java primitive type?

Pinchy
  • 1,506
  • 8
  • 28
  • 49

2 Answers2

6

A similar post I found today ..

Spring's Json not being resolved with appropriate response

Hope this might help

http://forum.springsource.org/showthread.php?85034-HttpMediaTypeNotAcceptableException-(always)

Community
  • 1
  • 1
Boopathi Rajaa
  • 4,659
  • 2
  • 31
  • 53
0

I am not sure which spring version you are using. I had the same problem and I solved it by adding the below jackson jars to my classpath.my spring version is 3.2.2

jackson-mapper-asl
jackson-core-asl

Here is my controller

@RequestMapping(value="/{name}", method = RequestMethod.GET)
public @ResponseBody List<Supplier> getSuppliers(@PathVariable String name) {


    searchDAO = (SearchDAO) SpringApplicationContext.getBean("searchDAO");
    List<Supplier> suppliers = null;
    try {
        suppliers = searchDAO.searchSuppliersByZipCode(name);
        //assertTrue(suppliers.size()>=1);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return suppliers;

}

I only have mvc annotation in my application context , there is no need for explicit content negotiation. When you have @ResponseBody , the default will json format and the jackson jars will be considered for conversion of your pojo.

Tito
  • 8,894
  • 12
  • 52
  • 86