SpringMVC返回带有JSON的对象列表

时间:2013-03-28 10:51:35

标签: java jquery json spring spring-mvc

我有两个选择选项,我希望当用户选择一个选项时,另一个选项填充数据库中的数据。但是我在将对象列表返回到视图时遇到了问题。

控制器

@RequestMapping(value="getCrimeTypeList.htm", method = RequestMethod.GET)
 public @ResponseBody List<CrimeType> getCrimeTypeList(@RequestParam(value="crimeCatId") Integer crimeCatId) throws Exception{              

        try {
            List<CrimeType> crimeTypeList = this.crimeTypeManager.getCrimeTypeList(crimeCatId);

             return crimeTypeList;
        } catch (Exception e) {

            logger.error(e.getMessage());
            return null;
        }
}

JQuery的

 $("select#offenceCatId").change(function(){

       $.ajax({
           type:'GET',
           url:'getCrimeTypeList.htm',
           data:{crimeCatId: $(this).val()},

            headers: {
             Accept: 'application/json'
            },
           dataType: 'json',

           success:function(data){

            alert('it worked');

           }

       });
     });

HTML

<li>
 <label>Offence Type</label>
 <form:select path="offenceTypeId" id="offenceTypeId" title="Offence Type">
 <form:options items="${crimeType.crimeTypeList}" itemValue="crimeTypeId" itemLabel="crimeTypeDesc"/>
 </form:select>
 <form:errors path="offenceTypeId" class="errors" />
</li>

错误

"NetworkError: 400 Bad Request - http://localhost:8084/crimeTrack/getCrimeTypeList.htm?[object%20Object]"

EDITED 我做了一些实验,发现如果Controller返回一个字符串,它会工作,但是一旦它返回一个Object,我就会遇到问题。

萤火

GET http://localhost:8084/crimeTrack/getCrimeTypeList.htm?crimeCatId=6 406 Not Acceptable

Response Headers
Content-Length  1067
Content-Type    text/html;charset=utf-8
Date    Fri, 29 Mar 2013 00:58:17 GMT
Server  Apache-Coyote/1.1

Request Headers
Accept  application/json
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Host    localhost:8084
Referer http://localhost:8084/crimeTrack/crime_registration.htm
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:19.0) Gecko/20100101 Firefox/19.0
X-Requested-With    XMLHttpRequest

2 个答案:

答案 0 :(得分:4)

首先请确保你的Spring版本是3.1.1版本,你已经在你的lib中添加了jackson.jar,然后尝试使用下面的代码,你的代码有一些reduntant。

@RequestMapping(value="/getCrimeTypeList.htm", method = RequestMethod.GET)
public @ResponseBody List<CrimeType> getCrimeTypeList(@RequestParam(value="crimeCatId") Integer crimeCatId) throws Exception{    
            try {
                return this.crimeTypeManager.getCrimeTypeList(crimeCatId);
                //return "true";
            } catch (Exception e) {
                logger.error(e.getMessage());
                return null;
            }
}
$("select#offenceCatId").change(function(){
        var param={crimeCatId:$(this).val()};
        $.ajax({
            type:'GET',
            url:'getCrimeTypeList.htm',
            data:param,
            success:function(data){
                //append options to list
            }
        });
});

答案 1 :(得分:2)

您的控制器需要一个请求标头Accept=application/json,如果您没有设置它。

尝试设置Accept标题

jQuery.ajax({
        type:'GET',
        url:'getCrimeTypeList.htm',
        data:{crimeCatId:$(this).val()},
        processData:false,
        headers: {
            Accept: 'application/json'
        },
        dataType: 'json',
        success:function(data){


            //append options to list


        }

    });