I've been googleing a bit around internet looking for a (correctly) way to @Consume an "application/json" file in a web resource on my server.
I'm using glassfish app server, so it's a java resource.
here is the calling javascvript code:
var url="/MBC/pages/lives/";
var mygetrequest=new ajaxRequest();
mygetrequest.onreadystatechange=function(){
if (mygetrequest.readyState==4){
if (mygetrequest.status==200 || window.location.href.indexOf("http")==-1){
var render="";
var JsonIn =JSON.parse(mygetrequest.responseText);
if(JsonIn.error==undefined){
render="generic error";
}
}else
render=mygetrequest.responseText ;
document.getElementById(div).innerHTML=render;
}else{
render="An error has occured making the request";
}
};
var json2Send = "{" +
"boss:\""+location.href.substring(location.href.length-5,location.href.length-4)+"\"," ;
if(document.newLive.bval.value=='')
json2Send+="bands:[],";
else
json2Send+="bands:["+document.newLive.bval.value+"],";
json2Send+="data:\""+document.newLive.dateEvent.value+"\"," +
"address:{street:\""+document.newLive.street.value+"\"," +
"number:\""+document.newLive.number.value+"\"," +
"city:\""+document.newLive.city.value+"\"," +
"region:\""+document.newLive.region.value+"\"," +
"state:\""+document.newLive.state.value+"\"}" +
"}";
mygetrequest.open("POST", url, true);
mygetrequest.setRequestHeader("Content-type", "application/json");
mygetrequest.send(json2Send);
where json2Send is the json String the client has to send to the server.
here is instead the server side code:
@POST
@Path("configLiveBand")
@Consumes("application/json")
@Produces("application/json")
public String liveBandInsert(String jsonIn, @Context HttpServletRequest request) throws ParseException{
I'm now asking to you what do I have to do in order to let the server read the input json string coming from the javascript. Obviously, the way I described above doesn't work. the server returns
HTTP Status 405 -
type Status report
message
descriptionThe specified HTTP method is not allowed for the requested resource ().
looking my problem over the internet, I've found solutions involving "readline()" method of the "BufferedReader" class. I do not like this solution. I prefer, if ther is a way, to inject the json file instead to read line by line the input string.
any help is well accepted thanks