AJAX用SpringMVC请求 - java.io.EOFException的:没有内容映射到对

2019-09-02 02:56发布

我工作的一个应用程序用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调用

 $('#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数据 -taken从萤火控制台

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

萤火 -内容被传递

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

Answer 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格式 。 例如,对于一个数组:

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


Answer 2:

这是改变,对我工作的控制器

@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;

    }


Answer 3:

如果不包括在头“的Content-Length”你会得到这个错误。



文章来源: SpringMVC ajax request - java.io.EOFException: No content to map to Object due to end of input