0

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

Dario Rusignuolo
  • 2,090
  • 6
  • 38
  • 68
  • of course, tell me if I have to explicit something else related to the problem I have that I haven't wrote yet. – Dario Rusignuolo Jun 25 '12 at 09:10
  • Does `liveBandInsert()` get invoked if you remove the `@Produces` and `@Consumes` annotations? What does the the request sent by your browser actually look like (check your browser's developer tools)? Does it work if you POST to the URL with same sample data using something like `curl`? Can you access the URL in your browser if you drop the `@Consumes` and `@Produces` and change to `@GET`? Please answer in the question, comments will probably too small :) – Philipp Reichart Jun 25 '12 at 09:15
  • if I remove `@Produces` and `@Consumes` annotations the server returns the same `405 status` back with the same description as before. you know what? the problem is `@Post` annotation. it works with get but does not with `@POST`. so, it's a javascript problem. I do not correctly the post request to the server. let me post the entire call... – Dario Rusignuolo Jun 25 '12 at 09:32
  • Why do you build your JSON using strings instead of actual Javascript Objects? Are the contents of `json2Send` actually valid JSON (check with an online validator like http://jsonlint.com/)? – Philipp Reichart Jun 25 '12 at 10:29
  • dear Philipp, you're absolutely correct. – Dario Rusignuolo Jun 25 '12 at 21:19

2 Answers2

0

the problem was the json as Philipp 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 of

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, post and send the json result back to the client. ;-) thanks Philipp

Dario Rusignuolo
  • 2,090
  • 6
  • 38
  • 68
0

Glad I could help.

I still recommend constructing your json2Send using actual Javascript Object Notation (JSON) instead of string concatenation, e.g. like this:

// 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's just another JS object again,
  // this time setting properties with literal notation key:value
  // Note how there's 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 converting it to a string for the HTTP POST like this:

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

This will catch syntax errors much earlier, relieves you from manually quoting all the different bits and pieces, is most likely faster and it sure makes the whole thing a lot more robust.

Philipp Reichart
  • 20,771
  • 6
  • 58
  • 65