Reputation: 5654
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
Upvotes: 0
Views: 9913
Reputation: 466
You'll get that error if you don't include "Content-Length" in the header.
Upvotes: 0
Reputation: 5654
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;
}
Upvotes: 1
Reputation: 1102
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"
}
]
Upvotes: 1