0

I am working on a SpringMVC application i requested some data from the database using an ajax call the data came back as json object. I now have to send this data back to the server for some processing and return to the form.

However i am getting an error in the browser The server encountered an internal error that prevented it from fulfilling this request. on investigating the error logs i saw this:

Error Log

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

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

Controller

@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 -taken from firebug console

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

FireBug - content is being passed

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
6
  • 1
    seems like you need to register your custom binder or just simply return String instead of List<Marker> Commented May 7, 2013 at 2:09
  • I am now returning just an object Marker. If i return a Object instead of a List<Object> will i still need to register a custom binder? Can you point me to a good example on how to do this Commented May 7, 2013 at 2:11
  • 1
    i did this code but i manually construct String to json format. you want that? Commented May 7, 2013 at 2:14
  • yea sure i will have a look at it i have been stuck on this for the longest while. The issue is as soon as the request reaches the controller this happens it does'nt enter the controller method Commented May 7, 2013 at 2:17
  • 1
    haha no man its test data Commented May 7, 2013 at 3:23

3 Answers 3

1

i did this code to preload getting allproduct for autocomplete function, maybe this example not fully fit with your code but i hope you can get something from this:

Controller function :

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

in my jsp, i use jquery to call that function :

$.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;
        }
    });
});

take a look for json format here. Example for an array :

[
    {
        "name": "Jason",
        "number": "10"
    },
    {
        "name": "Jimmy",
        "number": "11"
    }
]
Sign up to request clarification or add additional context in comments.

5 Comments

its processing json on the server thats give the issue but thanks for you code
hey did u take a look here stackoverflow.com/questions/3921736/… ?
yep i saw this question i do not understand how to register a custom data binder for this class can you help with that
but your errors didnt show binding error. how if u change param Marker citizens to straight String first.. did u will get that value? also take a look mkyong.com/java/how-to-convert-java-object-to-from-json-jackson for converting json to Java Object
Hey thanks i did a conversion from json object to java object and it works for me. Thanks so much for your help
1

This is the change to the controller that worked for me

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

    }

Comments

0

You'll get that error if you don't include "Content-Length" in the header.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.