Json message for java server

I searched the internet a bit, looking for a way (correctly) for @ Use the application / json file in a web resource on my server.

I am using Glassfish application server, so it is a java resource.

javascvript code is called here:

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 that the client should send to the server.

here server side code is used instead:

 @POST @Path("configLiveBand") @Consumes("application/json") @Produces("application/json") public String liveBandInsert(String jsonIn, @Context HttpServletRequest request) throws ParseException{ 

Now I ask you what I need to do for the server to read the json input string coming from javascript. Obviously, the method described above does not work. server returns

 HTTP Status 405 - type Status report message descriptionThe specified HTTP method is not allowed for the requested resource (). 

looking at my problem over the internet, I found solutions using the readline () method of the BufferedReader class. I do not like this solution. I prefer, if this is the way, to enter an input line by line instead of a json file.

any help is well received thanks

0
source share
2 answers

Glad I could help.

I still recommend creating your json2Send using the actual Javascript Object Notation (JSON) instead of string concatenation, for example. eg:

 // This creates an "empty" JS object, with no properties. var json2Send = new Object(); var length = location.href.length; // Adding a property is as easy as just setting it, it will be created by this. json2Send.boss = location.href.substring(length - 5, length - 4); if (document.newLive.bval.value == '') { json2Send.bands = []; } else { json2Send.bands = [document.newLive.bval.value]; } json2Send.data = document.newLive.dateEvent.value; // Properties can also be other objects, here created with the // object literal notation of { key:value, ...}. json2Send.address = { // Inside, it just another JS object again, // this time setting properties with literal notation key:value // Note how there no need to quote anything here! 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 }; 

and then convert it to a string for HTTP POST as follows:

 mygetrequest.open("POST", url, true); mygetrequest.setRequestHeader("Content-type", "application/json"); mygetrequest.send(JSON.stringify(json2Send)); 

This will cause syntax errors much earlier, save you from manually quoting all the different bits and parts, most likely faster, and this, of course, makes all this much more reliable.

0
source

The problem was in json, as Philippe wrote:

 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+"\"}" + "}"; 

instead

 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+"\"}" + "}"; 

now i can read, send and send json result to client. ;-) thanks to philip

0
source

All Articles