SpringMVC ajax请求 - java.io.EOFException:由于输入结束,没有要映射到Object的内容

时间:2013-05-06 23:46:01

标签: java spring spring-mvc

我正在开发SpringMVC应用程序我使用ajax调用从数据库请求了一些数据,数据作为json对象返回。我现在必须将这些数据发送回服务器进行一些处理并返回到表单。

但是我在浏览器中收到错误服务器遇到内部错误导致无法完成此请求。在调查错误日志时我看到了:

错误记录

Controller [com.crimetrack.web.MapController]
Method [public com.crimetrack.business.Marker com.crimetrack.web.MapController.getNewCoordinates(com.crimetrack.business.Marker) throws java.lang.Exception]

java.io.EOFException: No content to map to Object due to end of input
    at org.codehaus.jackson.map.ObjectMapper._initForReading(ObjectMapper.java:1324)
    at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:1275)
    at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:941)
    at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.readInternal(MappingJacksonHttpMessageConverter.java:124)
    at org.springframework.http.converter.AbstractHttpMessageConverter.read(AbstractHttpMessageConverter.java:153)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:120)
    at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodArgumentResolver.readWithMessageConverters(AbstractMessageConverterMethodArgumentResolver.java:91)
    at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.resolveArgument(RequestResponseBodyMethodProcessor.java:71)
    at org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75)
    at org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96)

Ajax Call

 $('#getCoordinates').on('click',function(){

        $.each(global_citizens, function(i, gc){

            $.ajax({
                type:'GET',
                url:'getNewCoordinatesForMarkers.htm',
                contentType: 'application/json',
                data:{citizens:JSON.stringify(global_citizens[i])},
                dataType: 'json',
                success:function(new_citizens){
                       $.each(new_citizens, function(i, c) {

                           console.log(c.name + ' | ' + c.socialSecurityNumber + ' | ' + c.lat+ ' | ' +c.lng);

                        });                                 
                }                           
            });             
        }); 
    });

控制器

@RequestMapping(value = "getNewCoordinatesForMarkers.htm", method = {RequestMethod.GET}, headers = {"content-type=application/json"})
public @ResponseBody  Marker getNewCoordinates(@RequestBody Marker citizens)throws Exception{

    logger.info("Getting Markers");
    Marker citizenMarker = this.markerManager.getNextLocation(citizens);

    return citizenMarker;

}

Marker.java

public class Marker  implements Serializable{

    private int socialSecurityNumber;
    private String name;
    private int citizenType;
    private double lat;
    private double lng;

//getters and setters

JSON DATA - 从firebug控制台获取

citizens{"name":"Jessie Small","lat":10.670044,"lng":-61.515305,"socialSecurityNumber":1999020214,"citizenType":3}

FireBug - 正在传递内容

Connection  close
Content-Length  3696
Content-Type    text/html;charset=utf-8
Date    Tue, 07 May 2013 05:52:09 GMT
Server  Apache-Coyote/1.1
Request Headers
Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language en-US,en;q=0.5
Content-Type    application/json
Cookie  tinymcePasteText=1; JSESSIONID=CC4F12D00C836FE0DB86D2493556275C
Host    localhost:8084
Referer http://localhost:8084/crimeTrack/crimeTrackMap.htm
User-Agent  Mozilla/5.0 (Windows NT 6.1; WOW64; rv:20.0) Gecko/20100101 Firefox/20.0
X-Requested-With    XMLHttpRequest

3 个答案:

答案 0 :(得分:1)

我做了这个代码预加载获取自动完成功能的所有产品,也许这个例子不完全适合你的代码,但我希望你能从中得到一些东西:

控制器功能:

@RequestMapping(value = "allproduct", method = RequestMethod.GET, headers = "Accept=*/*")
public @ResponseBody
String productList() {
    List<Product> products = ProductDAO.INSTANCE.listProduct();
    String json = "[";
    for (int i = 0; i < products.size(); i++) {
        Product o = products.get(i);
        if (i > 0) {
            json += ",";
        }
        json += "{\"value\":\"" + o.getCode() + "\",\"label\":\""
                + o.getCode() + " - " + o.getProvider() + " - "
                + o.getNominal() + "\",\"price\":\""
                + o.getPrice() + "\",\"cost\":\"" + o.getCost() + "\"}";
    }
    json += "]";
    System.out.println(json);
    return json;
}

在我的jsp中,我使用jquery来调用该函数:

$.getJSON('/product/allproduct', function(json) {
    $("#product").autocomplete({
        source : json,
        select : function(event, ui) {
            $("#product").val(ui.item.value);
            $("#kredit").val(ui.item.cost);
            $("#price").val(ui.item.price);
            return false;
        }
    });
});

查看json format here。数组示例:

[
    {
        "name": "Jason",
        "number": "10"
    },
    {
        "name": "Jimmy",
        "number": "11"
    }
]

答案 1 :(得分:1)

这是对我有用的控制器的更改

@RequestMapping(value = "getNewCoordinatesForMarkers.htm", method = {RequestMethod.POST},produces = "application/json; charset=utf-8")
    public @ResponseBody  Marker getNewCoordinates(@RequestBody Marker json)throws Exception{

        JSONObject jsonObj = JSONObject.fromObject(json);

        ObjectMapper mapper = new ObjectMapper();

         Marker citizen = mapper.readValue(jsonObj.toString(), new TypeReference<Marker>(){});


        logger.info("Getting Markers");
        Marker citizenMarker = this.markerManager.getNextLocation(citizen);

        return citizenMarker;

    }

答案 2 :(得分:0)

如果标题中未包含“Content-Length”,则会收到该错误。